home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_15.gml < prev    next >
Text File  |  1998-03-03  |  154KB  |  3,206 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Terminology title="Terminology">
  4. <page>
  5. <font size=14>
  6. B<br>
  7. binary search tree 
  8. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  9.  
  10. <a href=""><img src=iicons/bullbib.gif></a>
  11. <a href=""><img src=iicons/bullbib.gif></a>
  12. <a href=""><img src=iicons/bullbib.gif></a>
  13. <a href=""><img src=iicons/bullbib.gif></a>
  14. <br>
  15. binary tree 
  16. <a href="%s1p1"><img src=iicons/bullbib.gif></a>
  17. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  18. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  19. <br>
  20. binary tree sort 
  21. <a href=""><img src=iicons/bullbib.gif></a>
  22. <a href=""><img src=iicons/bullbib.gif></a>
  23. <br>
  24. C<br>
  25. child node 
  26. <a href=""><img src=iicons/bullbib.gif></a>
  27. <br>
  28. circular, doubly-linked 
  29. list 
  30. <a href="%s4p18"><img src=iicons/bullbib.gif></a>
  31. <br>
  32. circular, singly-linked 
  33. list 
  34. <a href="%s4p16"><img src=iicons/bullbib.gif></a>
  35. <br>
  36. D<br>
  37. deleting an item from a 
  38. binary tree 
  39. <a href="%s7p10"><img src=iicons/bullbib.gif></a>
  40. <br>
  41. </font>
  42.  
  43. </page>
  44. <page>
  45. <font size=14>
  46. <b>dequeue</b> 
  47. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  48. <br>
  49. doubly-linked list 
  50. <a href="%s4p16"><img src=iicons/bullbib.gif></a>
  51. <br>
  52. duplicate elimination 
  53.  
  54. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  55. <a href="^Exercises::c:s0p29"><img src=iicons/bullbib.gif></a>
  56. <br>
  57. dynamic data structures 
  58.  
  59. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  60. <br>
  61. dynamic memory 
  62. allocation 
  63. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  64. <a href="^Perform::c:s0p0"><img src=iicons/bullbib.gif></a>
  65. <br>
  66. E<br>
  67. <b>enqueue</b> 
  68. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  69. <br>
  70. F<br>
  71. first-in first-out (FIFO) 
  72.  
  73. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  74. <br>
  75. H<br>
  76. </font>
  77.  
  78. </page>
  79. <page>
  80. <font size=14>
  81. head of a queue 
  82. <a href="%s1p1"><img src=iicons/bullbib.gif></a>
  83. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  84. <br>
  85. I<br>
  86. inorder traversal 
  87. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  88.  
  89. <a href=""><img src=iicons/bullbib.gif></a>
  90. <br>
  91. insertion 
  92. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  93. <br>
  94. L<br>
  95. last-in first-out (LIFO) 
  96. data structure 
  97. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  98. <br>
  99. leaf node 
  100. <a href=""><img src=iicons/bullbib.gif></a>
  101. <a href=""><img src=iicons/bullbib.gif></a>
  102. <a href=""><img src=iicons/bullbib.gif></a>
  103. <br>
  104. left child 
  105. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  106. <br>
  107. left subtree 
  108. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  109. <a href="%s7p5"><img src=iicons/bullbib.gif></a>
  110.  
  111. <a href=""><img src=iicons/bullbib.gif></a>
  112. <a href=""><img src=iicons/bullbib.gif></a>
  113. <a href=""><img src=iicons/bullbib.gif></a>
  114. <a href=""><img src=iicons/bullbib.gif></a>
  115. <br>
  116. level-order binary tree 
  117. traversal 
  118. <a href=""><img src=iicons/bullbib.gif></a>
  119. <a href=""><img src=iicons/bullbib.gif></a>
  120. <br>
  121. </font>
  122.  
  123. </page>
  124. <page>
  125. <font size=14>
  126. linear data structure 
  127. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  128.  
  129. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  130. <br>
  131. linked list 
  132. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  133. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  134. <a href="%s4p2"><img src=iicons/bullbib.gif></a>
  135. <a href="%s4p15"><img src=iicons/bullbib.gif></a>
  136. <br>
  137. N<br>
  138. node 
  139. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  140. <br>
  141. nonlinear, two-
  142. dimensional data 
  143. structure 
  144. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  145. <br>
  146. null pointer (<b>0</b>) 
  147. <a href=""><img src=iicons/bullbib.gif></a>
  148. <a href=""><img src=iicons/bullbib.gif></a>
  149. <br>
  150. P<br>
  151. parent node 
  152. <a href=""><img src=iicons/bullbib.gif></a>
  153. <a href=""><img src=iicons/bullbib.gif></a>
  154.  
  155. <a href=""><img src=iicons/bullbib.gif></a>
  156. <br>
  157. <b>pop</b> 
  158. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  159. <br>
  160. </font>
  161.  
  162. </page>
  163. <page>
  164. <font size=14>
  165. postorder traversal 
  166. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  167.  
  168. <a href=""><img src=iicons/bullbib.gif></a>
  169. <a href=""><img src=iicons/bullbib.gif></a>
  170. <br>
  171. predicate function 
  172. <a href="%s4p5"><img src=iicons/bullbib.gif></a>
  173. <br>
  174. preorder traversal 
  175. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  176. <br>
  177. <b>push</b> 
  178. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  179. <br>
  180. Q<br>
  181. queue 
  182. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  183. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  184. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  185. <a href="%s4p15"><img src=iicons/bullbib.gif></a>
  186. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  187. <br>
  188. R<br>
  189. right child 
  190. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  191. <br>
  192. right subtree 
  193. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  194. <a href="%s7p5"><img src=iicons/bullbib.gif></a>
  195.  
  196. <a href=""><img src=iicons/bullbib.gif></a>
  197. <a href=""><img src=iicons/bullbib.gif></a>
  198. <a href=""><img src=iicons/bullbib.gif></a>
  199. <br>
  200. root node 
  201. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  202. <a href="%s7p5"><img src=iicons/bullbib.gif></a>
  203. <br>
  204. S<br>
  205. </font>
  206.  
  207. </page>
  208. <page>
  209. <font size=14>
  210. self-referential class 
  211. <a href="%s2p0"><img src=iicons/bullbib.gif></a>
  212.  
  213. <a href="^Illustration::c:s0p2"><img src=iicons/bullbib.gif></a>
  214. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  215. <br>
  216. sibling 
  217. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  218. <br>
  219. singly-linked list 
  220. <a href="%s4p16"><img src=iicons/bullbib.gif></a>
  221. <br>
  222. <b>sizeof</b> 
  223. <a href="%s3p1"><img src=iicons/bullbib.gif></a>
  224. <br>
  225. stack 
  226. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  227. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  228. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  229. <a href="%s4p15"><img src=iicons/bullbib.gif></a>
  230. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  231. <br>
  232. T<br>
  233. tail of a queue 
  234. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  235. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  236. <br>
  237. top of a stack 
  238. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  239. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  240. <br>
  241. traversal of a binary tree 
  242.  
  243. <a href="%s7p10"><img src=iicons/bullbib.gif></a>
  244. <br>
  245. tree 
  246. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  247. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  248. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  249. <br>
  250. <br>
  251. </font>
  252.  
  253. </page>
  254. </section>
  255. <section type=Popup name=Portable title="Portability">
  256. <page>
  257. A class object's size is 
  258. not necessarily the sum 
  259. of the sizes of its data 
  260. members. This is 
  261. because of various 
  262. machine-dependent 
  263. boundary alignment 
  264. requirements (see 
  265. Chapter 16) and other 
  266. reasons. Use operator <br>
  267.  
  268. </page>
  269. <page>
  270. <b>sizeof</b> to determine an 
  271. object's size.<br>
  272. <br>
  273.  
  274. </page>
  275. </section>
  276. <section type=Popup name=Quotes title="Quotes">
  277. <page>
  278. <i>Much that I bound, I 
  279. could not free;
  280. </i><br>
  281. <i>Much that I freed 
  282. returned to me.</i>  <br>
  283. Lee Wilson Dodd<br>
  284. <br>
  285.  
  286. </page>
  287. <page>
  288. <i>'Will you walk a little 
  289. faster?' said a whiting to 
  290. a snail,                       
  291. 'There's a porpoise close 
  292. behind us, and he's 
  293. treading on my tail.'</i> <br>
  294. Lewis Carroll<br>
  295. <br>
  296.  
  297. </page>
  298. <page>
  299. <i>There is always room at 
  300. the top.</i> <br>
  301. Daniel Webster<br>
  302. <br>
  303.  
  304. </page>
  305. <page>
  306. <i>Push on -- keep moving.</i> <br>
  307. Thomas Morton<br>
  308. <br>
  309.  
  310. </page>
  311. <page>
  312. <i>I think that I shall never 
  313. see                                       
  314. A poem lovely as a tree. </i> <br>
  315. Joyce Kilmer<br>
  316. <br>
  317.  
  318. </page>
  319. </section>
  320. <section type=Popup name=Illustration title="Illustrations">
  321. <page>
  322. <a href="^Illustration::c:s0p2">Fig. 15.1</a>  Two self-referential class objects linked together.<br>
  323. <a href="^Illustration::c:s0p3">Fig. 15.2</a>  A graphical representation of a list.<br>
  324. <a href="^Code::c:s0p0">Fig. 15.3</a>  Manipulating a linked list.<br>
  325. <a href="^Illustration::c:s0p4">Fig. 15.5</a>  The <b>insertAtFront</b> operation.<br>
  326. <a href="^Illustration::c:s0p5">Fig. 15.6</a>  A graphical representation of the <b>insertAtBack</b> operation.<br>
  327. <a href="^Illustration::c:s0p6">Fig. 15.7</a>  A graphical representation of the <b>removeFromFront</b> operation.<br>
  328. <a href="^Illustration::c:s0p7">Fig. 15.8</a>  A graphical representation of the <b>removeFromBack</b> operation.<br>
  329. <a href="^Code::c:s0p1">Fig. 15.9</a>  A simple stack program.<br>
  330. <a href="^Code::c:s0p2">Fig. 15.11</a>  A simple stack program using composition.<br>
  331. <a href="^Code::c:s0p3">Fig. 15.12</a>  Processing a queue.<br>
  332. <a href="^Illustration::c:s0p9">Fig. 15.14</a>  A graphical representation of a binary tree.<br>
  333. <a href="^Illustration::c:s0p8">Fig. 15.15</a>  A binary search tree.<br>
  334. <a href="^Code::c:s0p4">Fig. 15.16</a>  Creating and traversing a binary tree.<br>
  335. <a href="^Illustration::c:s0p10">Fig. 15.18</a>  A binary search tree.<br>
  336. <a href="^Illustration::c:s0p11">Fig. 15.19</a>  A 15-node binary search tree.<br>
  337. <a href="^Illustration::c:s0p12">Fig. 15.20</a>  Simple commands.<br>
  338. <a href="^Code::c:s0p7">Fig. 15.21</a>  Simple program that determines the sum of two integers.<br>
  339. <a href="^Code::c:s0p6">Fig. 15.22</a>  Simple program that finds the larger of two integers.<br>
  340. <a href="^Code::c:s0p5">Fig. 15.23</a>  Calculate the squares of several integers.<br>
  341.  
  342. </page>
  343. <page>
  344. <a href="^Illustration::c:s0p13">Fig. 15.24</a>  Writing, compiling, and executing a Simple language program.<br>
  345. <a href="^Illustration::c:s0p15">Fig. 15.25</a>  SML instructions produced after the compiler's first pass.<br>
  346. <a href="^Illustration::c:s0p14">Fig. 15.26</a>  Symbol table for program of Fig. 15.25.<br>
  347. <a href="^Illustration::c:s0p19">Fig. 15.27</a>  Unoptimized code from the program of Fig. 15.25.<br>
  348. <a href="^Illustration::c:s0p17">Fig. 15.28</a>  Optimized code for the program of Fig. 15.25.<br>
  349. <br>
  350.  
  351. </page>
  352. <page>
  353. <font size=18><a href="~audio/Ch15/15fig001.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.1 - Two self-referential class objects linked together.<img src="graphics/ch15/fig15001.gif" ></font><br>
  354.  
  355. </page>
  356. <page>
  357. <font size=18><a href="~audio/Ch15/15fig002.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.2 - A graphical representation of a list.<img src="graphics/ch15/fig15002.gif" ></font><br>
  358.  
  359. </page>
  360. <page>
  361. <font size=18><a href="~audio/Ch15/15fig005.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.5 - The <b>insertAtFront</b> operation.<img src="graphics/ch15/fig15005.gif" ></font><br>
  362.  
  363. </page>
  364. <page>
  365. <font size=18><a href="~audio/Ch15/15fig006.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.6 - A graphical representation of the <b>insertAtBack</b> operation.<img src="graphics/ch15/fig15006.gif" ></font><br>
  366.  
  367. </page>
  368. <page>
  369. <font size=18><a href="~audio/Ch15/15fig007.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.7 - A graphical representation of the <b>removeFromFront</b> operation.<img src="graphics/ch15/fig15007.gif" ></font><br>
  370.  
  371. </page>
  372. <page>
  373. <font size=18><a href="~audio/Ch15/15fig008.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.8 - A graphical representation of the <b>removeFromBack</b> operation.<img src="graphics/ch15/fig15008.gif" ></font><br>
  374.  
  375. </page>
  376. <page>
  377. <font size=18><a href="~audio/Ch15/15fig015.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.15 - A binary search tree.<img src="graphics/ch15/fig15015.gif" ></font><br>
  378.  
  379. </page>
  380. <page>
  381. <font size=18><a href="~audio/Ch15/15fig014.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.14 - A graphical representation of a binary tree.<img src="graphics/ch15/fig15014.gif" ></font><br>
  382.  
  383. </page>
  384. <page>
  385. <font size=18><a href="~audio/Ch15/15fig018.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 15.18 - A binary search tree.<img src="graphics/ch15/fig15018.gif" ></font><br>
  386.  
  387. </page>
  388. <page>
  389. <font size=18>Figure 15.19 - A 15-node binary search tree.<img src="graphics/ch15/fig15019.gif" ></font><br>
  390.  
  391. </page>
  392. <page>
  393. <font size=18>Figure 15.20 - Simple commands.<img src="graphics/ch15/fig15020.gif" ></font><br>
  394.  
  395. </page>
  396. <page>
  397. <font size=18>Figure 15.24 - Writing, compiling, and executing a Simple language program.<img src="graphics/ch15/fig15024.gif" ></font><br>
  398.  
  399. </page>
  400. <page>
  401. <font size=18>Figure 15.26 - Symbol table for program of <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.25</a>.<img src="graphics/ch15/fig15026.gif" ></font><br>
  402.  
  403. </page>
  404. <page>
  405. <font size=18>Figure 15.25 - SML instructions produced after the compiler's first pass. (Part 1 of 
  406. 2) </font><br>
  407. <img src="graphics/ch15/fig1525a.gif" ><br>
  408.  
  409. </page>
  410. <page>
  411. <font size=18>Figure 15.25 - SML instructions produced after the compiler's first pass. (Part 2 of 
  412. 2) </font><br>
  413. <img src="graphics/ch15/fig1525b.gif" ><br>
  414.  
  415. </page>
  416. <page>
  417. <font size=18>Figure 15.28 - Optimized code for the program of <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.25</a>. (Part 1 of 2) <img src="graphics/ch15/fig1528a.gif" ></font><br>
  418.  
  419. </page>
  420. <page>
  421. <font size=18>Figure 15.28 - Optimized code for the program of <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.25</a>. (Part 2 of 2) <img src="graphics/ch15/fig1528b.gif" ></font><br>
  422.  
  423. </page>
  424. <page>
  425. <font size=18>Figure 15.27 - Unoptimized code from the program of <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.25<b>.</b></a><img src="graphics/ch15/fig15027.gif" ></font><br>
  426.  
  427. </page>
  428. </section>
  429. <section type=Popup name=Answers title="Answers">
  430. <page pagename="Answer 15.1">
  431. <b>Answer 15.1</b><br>
  432. a) referential.<br>
  433. b) <b>new</b>.<br>
  434. c) stack.<br>
  435. d) predicate functions.<br>
  436. e) first-in-first-out (FIFO).<br>
  437. f) link.<br>
  438. g) <b>delete</b>.<br>
  439. h) queue.<br>
  440. i) tree.<br>
  441. j) last-in-first-out (LIFO).<br>
  442. <foreign  name="exercises" url="^Exercises::c:s0p0">
  443.  
  444. </page>
  445. <page pagename="Answer 15.1">
  446. k) binary.<br>
  447. l) root.<br>
  448. m) child or subtree.<br>
  449. n) leaf.<br>
  450. o) inorder, preorder, and postorder.<br>
  451. <foreign  name="exercises" url="^Exercises::c:s0p0">
  452. <br>
  453. <br>
  454.  
  455. </page>
  456. <page pagename="Answer 15.2">
  457. <b>Answer 15.2</b><br>
  458. It is possible to insert a node anywhere in a linked list and remove a node from 
  459. anywhere in a linked list. Nodes in a stack may only be inserted at the top of the 
  460. stack and removed from the top of a stack.<br>
  461. <foreign  name="exercises" url="^Exercises::c:s0p3">
  462. <br>
  463. <br>
  464.  
  465. </page>
  466. <page pagename="Answer 15.3">
  467. <b>Answer 15.3</b><br>
  468. A queue has pointers to both its head and its tail so that nodes may be inserted at 
  469. the tail and deleted from the head. A stack has a single pointer to the top of the 
  470. stack where both insertion and deletion of nodes are performed.<br>
  471. <foreign  name="exercises" url="^Exercises::c:s0p4">
  472. <br>
  473.  
  474. </page>
  475. <page pagename="Answer 15.4">
  476. <b>Answer 15.4</b><br>
  477. a)  Classes allow us to instantiate as many data structure objects of a certain type 
  478. (i.e., class) as we wish. <br>
  479. b)  Class templates enable us to instantiate related classes--each based on 
  480. different type parameters--we can then generate as many objects of each 
  481. template class as we like. <br>
  482. c)  Inheritance enables us to reuse code from a base class in a derived class so that 
  483. the derived-class data structure is also a base-class data structure (with public 
  484. inheritance, that is). <br>
  485. <br>
  486. <foreign  name="exercises" url="^Exercises::c:s0p5">
  487.  
  488. </page>
  489. <page pagename="Answer 15.4">
  490. d)  Private inheritance enables us to reuse portions of the code from a base class 
  491. to form a derived-class data structure; because the inheritance is <b>private</b>, all 
  492. <b>public</b> base-class member functions become <b>private</b> in the derived class. This 
  493. enables us to prevent clients of the derived-class data structure from accessing 
  494. base-class member functions that do not apply to the derived class. <br>
  495. e)  Composition enables us to reuse code by making a class object data structure a 
  496. member of a composed class; if we make the class object a <b>private</b> member of 
  497. the composed class, then the class object's <b>public</b> member functions are not 
  498. available through the composed object's interface.<br>
  499. <foreign  name="exercises" url="^Exercises::c:s0p5">
  500.  
  501. </page>
  502. <page pagename="Answer 15.5">
  503. <b>Answer 15.5</b><br>
  504. The inorder traversal is:<br>
  505. <font size=2><br></font><font size=11><pre>
  506. 11 18 19 28 32 40 44 49 69 71 72 83 92 97 99<p>
  507. </pre></font>
  508. The preorder traversal is:<br>
  509. <font size=2><br></font><font size=11><pre>
  510. 49 28 18 11 19 40 32 44 83 71 69 72 97 92 99<p>
  511. </pre></font>
  512. The postorder traversal is:<br>
  513. <font size=2><br></font><font size=11><pre>
  514. 11 19 18 32 44 40 28 69 72 71 92 99 97 83 49<p>
  515. </pre></font>
  516. <foreign  name="exercises" url="^Exercises::c:s0p6">
  517.  
  518. </page>
  519. <page pagename="Answer 15.6">
  520. <b>Answer 15.6</b><br>
  521. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  522. the file cpphtp2/answers/P15_06.zip to your hard drive and unzip the program 
  523. code.<br>
  524. <foreign  name="exercises" url="^Exercises::c:s0p7">
  525. <br>
  526.  
  527. </page>
  528. <page pagename="Answer 15.8">
  529. <b>Answer 15.8</b><br>
  530. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  531. the file cpphtp2/answers/P15_08.zip to your hard drive and unzip the program 
  532. code.<br>
  533. <foreign  name="exercises" url="^Exercises::c:s0p9">
  534. <br>
  535.  
  536. </page>
  537. <page pagename="Answer 15.10">
  538. <b>Answer 15.10</b><br>
  539. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  540. the file cpphtp2/answers/P15_10.zip to your hard drive and unzip the program 
  541. code.<br>
  542. <foreign  name="exercises" url="^Exercises::c:s0p11">
  543. <br>
  544.  
  545. </page>
  546. <page pagename="Answer 15.11">
  547. <b>Answer 15.11</b><br>
  548. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  549. the file cpphtp2/answers/P15_11.zip to your hard drive and unzip the program 
  550. code.<br>
  551. <foreign  name="exercises" url="^Exercises::c:s0p12">
  552. <br>
  553.  
  554. </page>
  555. <page pagename="Answer 15.17">
  556. <b>Answer 15.17</b><br>
  557. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  558. the file cpphtp2/answers/P15_17.zip to your hard drive and unzip the program 
  559. code.<br>
  560. <foreign  name="exercises" url="^Exercises::c:s0p28">
  561. <br>
  562.  
  563. </page>
  564. <page pagename="Answer 15.20">
  565. <b>Answer 15.20</b><br>
  566. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  567. the file cpphtp2/answers/P15_20.zip to your hard drive and unzip the program 
  568. code.<br>
  569. <foreign  name="exercises" url="^Exercises::c:s0p31">
  570. <br>
  571.  
  572. </page>
  573. <page pagename="Answer 15.24">
  574. <b>Answer 15.24</b><br>
  575. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  576. the file cpphtp2/answers/P15_24.zip to your hard drive and unzip the program 
  577. code.<br>
  578. <foreign  name="exercises" url="^Exercises::c:s0p40">
  579. <br>
  580.  
  581. </page>
  582. </section>
  583. <section type=Popup name=Exercises title="Exercises">
  584. <page pagename="Exercise 15.1">
  585. <b>Exercise 15.1</b><br>
  586. Fill in the blanks in each of the following:<br>
  587. a)  A self-________ class is used to form dynamic data structures. that can 
  588. grow and shrink at execution time<br>
  589. b)  Operator ________ is used to dynamically allocate memory; this operator 
  590. returns a pointer to the allocated memory.<br>
  591. c)  A ________ is a constrained version of a linked list in which nodes can be 
  592. inserted and deleted only from the start of the list; this data structure returns 
  593. node values in last-in-first-out order.<br>
  594. d)  A function that does not alter a linked list, but simply looks at the list to 
  595. determine if it is empty is referred to as a ________function.<br>
  596. <foreign  name="answers" url="^Answers::c:s0p0">
  597.  
  598. </page>
  599. <page pagename="Exercise 15.1">
  600. e)  A queue is referred to as a ________ data structure because the first nodes 
  601. inserted are the first nodes removed.<br>
  602. f)  The pointer to the next node in a linked list is referred to as a ________. <br>
  603. g)  Operator ________ is used to reclaim dynamically allocated memory.<br>
  604. h)  A ________ is a constrained version of a linked list in which nodes can be 
  605. inserted only at the end of the list and deleted only from the start of the list.<br>
  606. i)  A ________ is a nonlinear, two-dimensional data structure that contains 
  607. nodes with two or more links.<br>
  608. j)  A stack is referred to as a ________ data structure because the last node 
  609. inserted is the first node removed.<br>
  610. k)  The nodes of a ________ tree contain two link members.<br>
  611. l)  The first node of a tree is the ________ node.<br>
  612. <foreign  name="answers" url="^Answers::c:s0p0">
  613.  
  614. </page>
  615. <page pagename="Exercise 15.1">
  616. m)  Each link in a tree node points to a ________ or ________ of that node.<br>
  617. n)  A tree node that has no children is called a ________ node.<br>
  618. o)  The four traversal algorithms we mentioned in the text for binary search trees 
  619. are ______, ______, _______ and ______.<br>
  620. <foreign  name="answers" url="^Answers::c:s0p0">
  621. <br>
  622. <br>
  623.  
  624. </page>
  625. <page pagename="Exercise 15.2">
  626. <b>Exercise 15.2</b><br>
  627. What are the differences between a linked list and a stack?<br>
  628. <foreign  name="answers" url="^Answers::c:s0p2">
  629. <br>
  630. <br>
  631.  
  632. </page>
  633. <page pagename="Exercise 15.3">
  634. <b>Exercise 15.3</b><br>
  635. What are the differences between a stack and a queue?<br>
  636. <foreign  name="answers" url="^Answers::c:s0p3">
  637. <br>
  638. <br>
  639.  
  640. </page>
  641. <page pagename="Exercise 15.4">
  642. <b>Exercise 15.4</b><br>
  643. Perhaps a more appropriate title for this chapter would have been "Reusable 
  644. Data Structures." Comment on how each of the following entities or concepts 
  645. contributes to the reusability of data structures: <br>
  646. a)  classes<br>
  647. b)  template classes<br>
  648. c)  inheritance<br>
  649. d)  private inheritance<br>
  650. e)  composition.<br>
  651. <foreign  name="answers" url="">
  652.  
  653. </page>
  654. <page pagename="Exercise 15.5">
  655. <b>Exercise 15.5</b><br>
  656. Manually provide the inorder, preorder, and postorder traversals of the binary 
  657. search tree of <a href="^Illustration::c:s0p11"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.19</a>.<br>
  658. <foreign  name="answers" url="^Answers::c:s0p6">
  659. <br>
  660. <br>
  661.  
  662. </page>
  663. <page pagename="Exercise 15.6">
  664. <b>Exercise 15.6</b><br>
  665. Write a program that concatenates two linked list objects of characters. The 
  666. program should include function <b>concatenate</b> that takes references to both list 
  667. objects as arguments and concatenates the second list to the first list.<br>
  668. <br>
  669. <foreign  name="answers" url="^Answers::c:s0p7">
  670. <br>
  671.  
  672. </page>
  673. <page pagename="Exercise 15.7">
  674. <b>Exercise 15.7</b><br>
  675. Write a program that merges two ordered list objects of integers into a single 
  676. ordered list object of integers. Function <b>merge</b> should receive references to each 
  677. of the list objects to be merged, and should return a reference to the merged list 
  678. object.<br>
  679. <br>
  680. <br>
  681.  
  682. </page>
  683. <page pagename="Exercise 15.8">
  684. <b>Exercise 15.8</b><br>
  685. Write a program that inserts 25 random integers from 0 to 100 in order in a 
  686. linked list object. The program should calculate the sum of the elements, and the 
  687. floating-point average of the elements.<br>
  688. <br>
  689. <foreign  name="answers" url="^Answers::c:s0p8">
  690. <br>
  691.  
  692. </page>
  693. <page pagename="Exercise 15.9">
  694. <b>Exercise 15.9</b><br>
  695. Write a program that creates a linked list object of 10 characters, then creates a 
  696. second list object containing a copy of the first list, but in reverse order. <br>
  697. <br>
  698. <br>
  699.  
  700. </page>
  701. <page pagename="Exercise 15.10">
  702. <b>Exercise 15.10</b><br>
  703. Write a program that inputs a line of text and uses a stack object to print the line 
  704. reversed.<br>
  705. <br>
  706. <foreign  name="answers" url="^Answers::c:s0p9">
  707. <br>
  708.  
  709. </page>
  710. <page pagename="Exercise 15.11">
  711. <b>Exercise 15.11</b><br>
  712. Write a program that uses a stack object to determine if a string is a palindrome 
  713. (i.e., the string is spelled identically backwards and forwards). The program 
  714. should ignore spaces and punctuation.<br>
  715. <br>
  716. <foreign  name="answers" url="^Answers::c:s0p10">
  717. <br>
  718.  
  719. </page>
  720. <page pagename="Exercise 15.12">
  721. <b>Exercise 15.12</b><br>
  722. Stacks are used by compilers to help in the process of evaluating expressions 
  723. and generating machine language code. In this and the next exercise, we 
  724. investigate how compilers evaluate arithmetic expressions consisting only of 
  725. constants, operators, and parentheses.<br>
  726. Humans generally write expressions like <b>3 + 4</b> and <b>7 / 9</b> in which the operator (<b>+</b> 
  727. or <b>/</b> here) is written between its operands--this is called <i>infix notation</i>. 
  728. Computers "prefer" <i>postfix notation</i> in which the operator is written to the right 
  729. of its two operands. The preceding infix expressions would appear in postfix 
  730. notation as <b>3 4 +</b> and <b>7 9 /</b>, respectively.<br>
  731. To evaluate a complex infix expression, a compiler would first convert the 
  732. expression to postfix notation, and then evaluate the postfix version of the <br>
  733.  
  734. </page>
  735. <page pagename="Exercise 15.12">
  736. expression. Each of these algorithms requires only a single left-to-right pass of 
  737. the expression. Each algorithm uses a stack object in support of its operation, 
  738. and in each algorithm the stack is used for a different purpose.<br>
  739. In this exercise, you will write a C++ version of the infix-to-postfix conversion 
  740. algorithm. In the next exercise, you will write a C++ version of the postfix 
  741. expression evaluation algorithm. Later in the chapter, you will discover that 
  742. code you write in this exercise can help you implement a complete working 
  743. compiler.<br>
  744. Write a program that converts an ordinary infix arithmetic expression (assume a 
  745. valid expression is entered) with single digit integers such as <br>
  746. <font size=2><br></font><font size=11><pre>
  747. (6 + 2) * 5 - 8 / 4<p>
  748. </pre></font>
  749. to a postfix expression. The postfix version of the preceding infix expression is<br>
  750.  
  751. </page>
  752. <page pagename="Exercise 15.12">
  753. <font size=2><br></font><font size=11><pre>
  754. 6 2 + 5 * 8 4 / -<p>
  755. </pre></font>
  756. The program should read the expression into character array <b>infix</b>, and use 
  757. modified versions of the stack functions implemented in this chapter to help 
  758. create the postfix expression in character array <b>postfix</b>. The algorithm for 
  759. creating a postfix expression is as follows:<br>
  760. 1. Push a left parenthesis '<b>(</b>' onto the stack.<br>
  761. 2. Append a right parenthesis '<b>)</b>' to the end of <b>infix</b>.<br>
  762. While the stack is not empty, read <b>infix</b> from left to right and do the following:<p>
  763. If the current character in <b>infix</b> is a digit, copy it to the next element of <b>postfix</b>.<p>
  764. If the current character in <b>infix</b> is a left parenthesis, push it onto the stack.<p>
  765. If the current character in <b>infix</b> is an operator,<p><br>
  766.  
  767. </page>
  768. <page pagename="Exercise 15.12">
  769. 3. <spacer width=20 height=1><spacer width=20 height=1>Pop operators (if there are any) at the top of the stack while they have equal or<p>
  770. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>higher precedence than the current operator, and insert the popped<p>
  771. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>operators in <b>postfix</b>.<p>
  772. <spacer width=20 height=1><spacer width=20 height=1>Push the current character in <b>infix</b> onto the stack.<p>
  773. If the current character in <b>infix</b> is a right parenthesis<p>
  774. <spacer width=20 height=1><spacer width=20 height=1>Pop operators from the top of the stack and insert them in <b>postfix</b> until a left<p>
  775. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>parenthesis is at the top of the stack.<p>
  776. <spacer width=20 height=1><spacer width=20 height=1>Pop (and discard) the left parenthesis from the stack.<br>
  777. The following arithmetic operations are allowed in an expression:<br>
  778. <b>+</b>addition<br>
  779. <b>-</b>subtraction<br>
  780. <b>*</b>multiplication<br>
  781.  
  782. </page>
  783. <page pagename="Exercise 15.12">
  784. <b>/</b>division<br>
  785. <b>^</b>exponentiation<br>
  786. <b>%</b>modulus<br>
  787. The stack should be maintained with stack nodes that each contain a data 
  788. member and a pointer to the next stack node.<br>
  789. Some of the functional capabilities you may want to provide are:<br>
  790. a)  Function <b>convertToPostfix</b> that converts the infix expression to postfix 
  791. notation.<br>
  792. b)  Function <b>isOperator</b> that determines if <b>c</b> is an operator.<br>
  793. c)  Function <b>precedence</b> that determines if the precedence of <b>operator1</b> is less 
  794. than, equal to, or greater than the precedence of <b>operator2</b>. The function returns 
  795. -1, 0, and 1, respectively.<br>
  796.  
  797. </page>
  798. <page pagename="Exercise 15.12">
  799. d)  Function <b>push</b> that pushes a value onto the stack.<br>
  800. e)  Function <b>pop</b> that pops a value off the stack.<br>
  801. f)  Function <b>stackTop</b> that returns the top value of the stack without popping the 
  802. stack.<br>
  803. g)  Function <b>isEmpty</b> that determines if the stack is empty.<br>
  804. h)  Function <b>printStack</b> that prints the stack.<br>
  805. <br>
  806. <br>
  807.  
  808. </page>
  809. <page pagename="Exercise 15.13">
  810. <b>Exercise 15.13</b><br>
  811. Write a program that evaluates a postfix expression (assume it is valid) such as <br>
  812. <font size=2><br></font><font size=11><pre>
  813. 6 2 + 5 * 8 4 / -<p>
  814. </pre></font>
  815. The program should read a postfix expression consisting of digits and operators 
  816. into a character array. Using modified versions of the stack functions 
  817. implemented earlier in this chapter, the program should scan the expression and 
  818. evaluate it. The algorithm is as follows:<br>
  819. 1. Append the null character ('<b>\\0</b>') to the end of the postfix expression. When 
  820. the null character is encountered, no further processing is necessary.<br>
  821. While '<b>\\0</b>' has not been encountered, read the expression from left to right.<p>
  822. <spacer width=20 height=1><spacer width=20 height=1>If the current character is a digit,<p>
  823. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Push its integer value onto the stack (the integer value of a digit character is <br>
  824.  
  825. </page>
  826. <page pagename="Exercise 15.13">
  827. 2. its <p>
  828. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>value in the computer's character set minus the value of ' <b>0</b>' in the computer's<p>
  829. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>character set).<p>
  830. <spacer width=20 height=1><spacer width=20 height=1>Otherwise, if the current character is an <i>operator</i>,<p>
  831. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Pop the two top elements of the stack into variables x and y.<p>
  832. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Calculate <b>y</b> <i>operator</i> <b>x</b>.<p>
  833. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Push the result of the calculation onto the stack.<br>
  834. 3. When the null character is encountered in the expression, pop the top value of 
  835. the stack. This is the result of the postfix expression.<br>
  836. Note: In 2) above, if the operator is '<b>/</b>', the top of the stack is <b>2</b>, and the next 
  837. element in the stack is <b>8</b>, then pop <b>2</b> into <b>x</b>, pop <b>8</b> into <b>y</b>, evaluate <b>8 / 2</b>, and push <br>
  838.  
  839. </page>
  840. <page pagename="Exercise 15.13">
  841. the result, <b>4</b>, back onto the stack. This note also applies to operator '<b>-</b>'. The 
  842. arithmetic operations allowed in an expression are:<br>
  843. <b>+</b>addition<br>
  844. <b>-</b>subtraction<br>
  845. <b>*</b>multiplication<br>
  846. <b>/</b>division<br>
  847. <b>^</b>exponentiation<br>
  848. <b>%</b>modulus<br>
  849. The stack should be maintained with stack nodes that contain an <b>int</b> data 
  850. member and a pointer to the next stack node. You may want to provide the 
  851. following functional capabilities:<br>
  852. a)  Function <b>evaluatePostfixExpression</b> that evaluates the postfix expression.<br>
  853.  
  854. </page>
  855. <page pagename="Exercise 15.13">
  856. b)  Function <b>calculate</b> that evaluates the expression <b>op1</b> <b>operator</b> <b>op2</b>.<br>
  857. c)  Function <b>push</b> that pushes a value onto the stack.<br>
  858. d)  Function <b>pop</b> that pops a value off the stack.<br>
  859. e)  Function <b>isEmpty</b> that determines if the stack is empty.<br>
  860. f)  Function <b>printStack</b> that prints the stack.<br>
  861. <br>
  862. <br>
  863.  
  864. </page>
  865. <page pagename="Exercise 15.14">
  866. <b>Exercise 15.14</b><br>
  867. Modify the postfix evaluator program of<a href="^Exercises::c:s0p19"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.13</a> so that it can process 
  868. integer operands larger than 9.<br>
  869. <br>
  870. <br>
  871.  
  872. </page>
  873. <page pagename="Exercise 15.15">
  874. <b>Exercise 15.15</b><br>
  875. (<i>Supermarket simulation</i>) Write a program that simulates a check-out line at a 
  876. supermarket. The line is a queue object. Customers (i.e., customer objects) 
  877. arrive in random integer intervals of 1 to 4 minutes. Also, each customer is 
  878. serviced in random integer intervals of 1 to 4 minutes. Obviously, the rates need 
  879. to be balanced. If the average arrival rate is larger than the average service rate, 
  880. the queue will grow infinitely. Even with "balanced" rates, randomness can still 
  881. cause long lines. Run the supermarket simulation for a 12-hour day (720 
  882. minutes) using the following algorithm:<br>
  883.  
  884. </page>
  885. <page pagename="Exercise 15.15">
  886. 1. Choose a random integer between 1 and 4 to determine the minute at which 
  887. the first customer arrives. <br>
  888. 2. At the first customer's arrival time:<p>
  889. Determine customer's service time (random integer from 1 to 4);<p>
  890. Begin servicing the customer;<p>
  891. Schedule arrival time of next customer (random integer 1 to 4 added to the current time).<br>
  892. For each minute of the day:<p>
  893. <spacer width=20 height=1><spacer width=20 height=1>If the next customer arrives,<p>
  894. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Say so,<p>
  895. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Enqueue the customer;<p>
  896. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Schedule the arrival time of the next customer;<p><br>
  897.  
  898. </page>
  899. <page pagename="Exercise 15.15">
  900. 3. <spacer width=20 height=1><spacer width=20 height=1>If service was completed for the last customer;<p>
  901. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Say so<p>
  902. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Dequeue next customer to be serviced<p>
  903. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Determine customer's service completion time (random integer from 1 to 4<p>
  904. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>added to the current time).<br>
  905. Now run your simulation for 720 minutes and answer each of the following:<br>
  906. a)  What is the maximum number of customers in the queue at any time?<br>
  907. b)  What is the longest wait any one customer experiences?<br>
  908. c)  What happens if the arrival interval is changed from 1-to-4 minutes to 1-to-3 
  909. minutes?<br>
  910. <br>
  911. <br>
  912.  
  913. </page>
  914. <page pagename="Exercise 15.16">
  915. <b>Exercise 15.16</b><br>
  916. Modify the program of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.16</a> to allow the binary tree object to contain 
  917. duplicates.<br>
  918. <br>
  919. <br>
  920.  
  921. </page>
  922. <page pagename="Exercise 15.17">
  923. <b>Exercise 15.17</b><br>
  924. Write a program based on the program of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.16</a> that inputs a line of text, 
  925. tokenizes the sentence into separate words (you may want to use the <b>strtok</b> 
  926. library function), inserts the words in a binary search tree, and prints the inorder, 
  927. preorder, and postorder traversals of the tree. Use an OOP approach.<br>
  928. <br>
  929. <foreign  name="answers" url="^Answers::c:s0p11">
  930. <br>
  931.  
  932. </page>
  933. <page pagename="Exercise 15.18">
  934. <b>Exercise 15.18</b><br>
  935. In this chapter, we saw that duplicate elimination is straightforward when 
  936. creating a binary search tree. Describe how you would perform duplicate 
  937. elimination using only a single-subscripted array. Compare the performance of 
  938. array-based duplicate elimination with the performance of binary-search-tree-
  939. based duplicate elimination.<br>
  940. <br>
  941.  
  942. </page>
  943. <page pagename="Exercise 15.19">
  944. <b>Exercise 15.19</b><br>
  945. Write a function <b>depth</b> that receives a binary tree and determines how many 
  946. levels it has.<br>
  947. <br>
  948. <br>
  949.  
  950. </page>
  951. <page pagename="Exercise 15.20">
  952. <b>Exercise 15.20</b><br>
  953. (<i>Recursively print a list backwards</i>) Write a member function 
  954. <b>printListBackwards</b> that recursively outputs the items in a linked list object in 
  955. reverse order. Write a test program that creates a sorted list of integers and prints 
  956. the list in reverse order. <br>
  957. <br>
  958. <foreign  name="answers" url="^Answers::c:s0p12">
  959. <br>
  960.  
  961. </page>
  962. <page pagename="Exercise 15.21">
  963. <b>Exercise 15.21</b><br>
  964. (<i>Recursively search a list</i>) Write a member function <b>searchList</b> that recursively 
  965. searches a linked list object for a specified value. The function should return a 
  966. pointer to the value if it is found; otherwise, null should be returned. Use your 
  967. function in a test program that creates a list of integers. The program should 
  968. prompt the user for a value to locate in the list. <br>
  969. <br>
  970. <br>
  971.  
  972. </page>
  973. <page pagename="Exercise 15.22">
  974. <b>Exercise 15.22</b><br>
  975. (<i>Binary tree delete</i>) In this exercise, we discuss deleting items from binary 
  976. search trees. The deletion algorithm is not as straightforward as the insertion 
  977. algorithm. There are three cases that are encountered when deleting an item--
  978. the item is contained in a leaf node (i.e., it has no children), the item is contained 
  979. in a node that has one child, or the item is contained in a node that has two 
  980. children. <br>
  981. If the item to be deleted is contained in a leaf node, the node is deleted and the 
  982. pointer in the parent node is set to null. <br>
  983. If the item to be deleted is contained in a node with one child, the pointer in the 
  984. parent node is set to point to the child node and the node containing the data item <br>
  985.  
  986. </page>
  987. <page pagename="Exercise 15.22">
  988. is deleted. This causes the child node to take the place of the deleted node in the 
  989. tree. <br>
  990. The last case is the most difficult. When a node with two children is deleted, 
  991. another node in the tree must take its place. However, the pointer in the parent 
  992. node cannot simply be assigned to point to one of the children of the node to be 
  993. deleted. In most cases, the resulting binary search tree would not adhere to the 
  994. following characteristic of binary search trees (with no duplicate values): <i>The 
  995. values in any left subtree are less than the value in the parent node, and the 
  996. values in any right subtree are greater than the value in the parent node</i>. <br>
  997. Which node is used as a <i>replacement node</i> to maintain this characteristic? Either 
  998. the node containing the largest value in the tree less than the value in the node 
  999. being deleted, or the node containing the smallest value in the tree greater than <br>
  1000.  
  1001. </page>
  1002. <page pagename="Exercise 15.22">
  1003. the value in the node being deleted. Let us consider the node with the smaller 
  1004. value. In a binary search tree, the largest value less than a parent's value is 
  1005. located in the left subtree of the parent node and is guaranteed to be contained in 
  1006. the rightmost node of the subtree. This node is located by walking down the left 
  1007. subtree to the right until the pointer to the right child of the current node is null. 
  1008. We are now pointing to the replacement node which is either a leaf node or a 
  1009. node with one child to its left. If the replacement node is a leaf node, the steps to 
  1010. perform the deletion are as follows:<br>
  1011. 1. Store the pointer to the node to be deleted in a temporary pointer variable (this 
  1012. pointer is used to delete the dynamically allocated memory)<br>
  1013. 2. Set the pointer in the parent of the node being deleted to point to the replacement node<br>
  1014.  
  1015. </page>
  1016. <page pagename="Exercise 15.22">
  1017. 3. Set the pointer in the parent of the replacement node to null<br>
  1018. 4. Set the pointer to the right subtree in the replacement node to point to the right 
  1019. subtree of the node to be deleted<br>
  1020. 5. Delete the node to which the temporary pointer variable points.<br>
  1021. The deletion steps for a replacement node with a left child are similar to those 
  1022. for a replacement node with no children, but the algorithm also must move the 
  1023. child in to the replacement node's position in the tree. If the replacement node is 
  1024. a node with a left child, the steps to perform the deletion are as follows:<br>
  1025. 1. Store the pointer to the node to be deleted in a temporary pointer variable<br>
  1026. 2. Set the pointer in the parent of the node being deleted to point to the replacement node<br>
  1027.  
  1028. </page>
  1029. <page pagename="Exercise 15.22">
  1030. 3. Set the pointer in the parent of the replacement node to point to the left child 
  1031. of the replacement node<br>
  1032. 4. Set the pointer to the right subtree in the replacement node to point to the right 
  1033. subtree of the node to be deleted<br>
  1034. 5. Delete the node to which the temporary pointer variable points.<br>
  1035. Write member function <b>deleteNode</b> which takes as its arguments a pointer to the 
  1036. root node of the tree object and the value to be deleted. The function should 
  1037. locate in the tree the node containing the value to be deleted and use the 
  1038. algorithms discussed here to delete the node. If the value is not found in the tree, 
  1039. the function should print a message that indicates whether or not the value is 
  1040. deleted. Modify the program of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.16</a> to use this function. After deleting an <br>
  1041.  
  1042. </page>
  1043. <page pagename="Exercise 15.22">
  1044. item, call the <b>inOrder</b>, <b>preOrder</b>, and <b>postOrder</b> traversal functions to confirm 
  1045. that the delete operation was performed correctly.<br>
  1046. <br>
  1047. <br>
  1048.  
  1049. </page>
  1050. <page pagename="Exercise 15.23">
  1051. <b>Exercise 15.23</b><br>
  1052. (<i>Binary tree search</i>) Write member function <b>binaryTreeSearch</b> that attempts to 
  1053. locate a specified value in a binary search tree object. The function should take 
  1054. as arguments a pointer to the root node of the binary tree and a search key to be 
  1055. located. If the node containing the search key is found, the function should 
  1056. return a pointer to that node; otherwise, the function should return a null pointer.<br>
  1057. <br>
  1058. <br>
  1059.  
  1060. </page>
  1061. <page pagename="Exercise 15.24">
  1062. <b>Exercise 15.24</b><br>
  1063. (<i>Level-order binary tree traversal</i>) The program of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.16</a> illustrated three 
  1064. recursive methods of traversing a binary tree--inorder, preorder, and postorder 
  1065. traversals. This exercise presents the <i>level-order traversal</i> of a binary tree in 
  1066. which the node values are printed level-by-level starting at the root node level. 
  1067. The nodes on each level are printed from left to right. The level-order traversal is 
  1068. not a recursive algorithm. It uses a queue object to control the output of the 
  1069. nodes. The algorithm is as follows:<br>
  1070. 1. Insert the root node in the queue<br>
  1071. 2. While there are nodes left in the queue,<p>
  1072. <spacer width=20 height=1><spacer width=20 height=1>Get the next node in the queue<p>
  1073. <spacer width=20 height=1><spacer width=20 height=1>Print the node's value<br>
  1074. <foreign  name="answers" url="^Answers::c:s0p13">
  1075.  
  1076. </page>
  1077. <page pagename="Exercise 15.24">
  1078. <spacer width=20 height=1><spacer width=20 height=1>If the pointer to the left child of the node is not null<p>
  1079. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Insert the left child node in the queue<p>
  1080. <spacer width=20 height=1><spacer width=20 height=1>If the pointer to the right child of the node is not null<p>
  1081. <spacer width=20 height=1><spacer width=20 height=1><spacer width=20 height=1>Insert the right child node in the queue.<br>
  1082. Write member function <b>levelOrder</b> to perform a level-order traversal of a binary 
  1083. tree object. Modify the program of<a href="^Code::c:s0p4"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig 15.16</a> to use this function. (Note: You 
  1084. will also need to modify and incorporate the queue processing functions of <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 
  1085. 15.12</a> in this program.)<br>
  1086. <foreign  name="answers" url="^Answers::c:s0p13">
  1087.  
  1088. </page>
  1089. <page pagename="Exercise 15.25">
  1090. <b>Exercise 15.25</b><br>
  1091. (<i>Printing trees</i>) Write a recursive member function <b>outputTree</b> to display a 
  1092. binary tree object on the screen. The function should output the tree row-by-row 
  1093. with the top of the tree at the left of the screen and the bottom of the tree toward 
  1094. the right of the screen. Each row is output vertically. For example, the binary 
  1095. tree illustrated in <a href="^Illustration::c:s0p11"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.19</a> is output as follows:<br>
  1096.  
  1097. </page>
  1098. <page pagename="Exercise 15.25">
  1099. <img src="graphics/ch15/ex15025.gif" ><br>
  1100.  
  1101. </page>
  1102. <page pagename="Exercise 15.25">
  1103. Note the rightmost leaf node appears at the top of the output in the rightmost 
  1104. column and the root node appears at the left of the output. Each column of 
  1105. output starts five spaces to the right of the previous column. Function 
  1106. <b>outputTree</b> should receive an argument <b>totalSpaces</b> representing the number of 
  1107. spaces preceding the value to be output (this variable should start at zero so the 
  1108. root node is output at the left of the screen). The function uses a modified 
  1109. inorder traversal to output the tree--it starts at the rightmost node in the tree and 
  1110. works back to the left. The algorithm is as follows:<br>
  1111. While the pointer to the current node is not null<br>
  1112. Recursively call <b>outputTree</b> with the right subtree of the current node and 
  1113. <b>totalSpaces + 5
  1114. </b><br>
  1115. Use a <b>for</b> structure to count from <b>1</b> to <b>totalSpaces</b> and output spaces<br>
  1116.  
  1117. </page>
  1118. <page pagename="Exercise 15.25">
  1119. Output the value in the current node<br>
  1120. Set the pointer to the current node to point to the left subtree of the current node 
  1121. Increment <b>totalSpaces</b> by 5.<br>
  1122. <br>
  1123. <br>
  1124.  
  1125. </page>
  1126. <page pagename="Special Section: Building Your Own Compiler">
  1127. <b>Special Section: Building Your Own Compiler</b><br>
  1128. In Exercises 5.18 and 5.19, we introduced Simpletron Machine Language 
  1129. (SML) and you implemented a Simpletron computer simulator to execute 
  1130. programs written in SML. In this section, we build a compiler that converts 
  1131. programs written in a high-level programming language to SML. This section 
  1132. "ties" together the entire programming process. You will write programs in this 
  1133. new high-level language, compile these programs on the compiler you build, 
  1134. and run the programs on the simulator you built in Exercise 7.19. You should 
  1135. make every effort to implement your compiler in an object-oriented manner.<br>
  1136.  
  1137. </page>
  1138. <page pagename="Exercise 15.26">
  1139. <b>Exercise 15.26</b><br>
  1140. (The Simple Language) Before we begin building the compiler, we discuss a 
  1141. simple, yet powerful, high-level language similar to early versions of the 
  1142. popular language BASIC. We call the language <i>Simple</i>. Every Simple <i>statement</i> 
  1143. consists of a <i>line number</i> and a Simple <i>instruction</i>. Line numbers must appear in 
  1144. ascending order. Each instruction begins with one of the following Simple 
  1145. <i>commands</i>: <b>rem</b>, <b>input</b>, <b>let</b>, <b>print</b>, <b>goto</b>, <b>if/goto</b>, or <b>end</b> (see <a href="^Illustration::c:s0p12"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.20</a>). All 
  1146. commands except end can be used repeatedly. Simple evaluates only integer 
  1147. expressions using the <b>+</b>, <b>-</b>, <b>*</b>, and <b>/</b> operators. These operators have the same 
  1148. precedence as in C. Parentheses can be used to change the order of evaluation of 
  1149. an expression.<br>
  1150.  
  1151. </page>
  1152. <page pagename="Exercise 15.26">
  1153. Our Simple compiler recognizes only lowercase letters. All characters in a 
  1154. Simple file should be lowercase (uppercase letters result in a syntax error unless 
  1155. they appear in a <b>rem</b> statement in which case they are ignored). A <i>variable name</i> 
  1156. is a single letter. Simple does not allow descriptive variable names, so variables 
  1157. should be explained in remarks to indicate their use in a program. Simple uses 
  1158. only integer variables. Simple does not have variable declarations--merely 
  1159. mentioning a variable name in a program causes the variable to be declared and 
  1160. initialized to zero automatically. The syntax of Simple does not allow string 
  1161. manipulation (reading a string, writing a string, comparing strings, etc.). If a 
  1162. string is encountered in a Simple program (after a command other than <b>rem</b>), the 
  1163. compiler generates a syntax error. The first version of our compiler will assume <br>
  1164.  
  1165. </page>
  1166. <page pagename="Exercise 15.26">
  1167. that Simple programs are entered correctly. <a href="^Exercises::c:s0p84"><img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.29</a> asks the student to 
  1168. modify the compiler to perform syntax error checking.<br>
  1169. Simple uses the conditional <b>if/goto</b> statement and the unconditional <b>goto</b> 
  1170. statement to alter the flow of control during program execution. If the condition 
  1171. in the <b>if/goto</b> statement is true, control is transferred to a specific line of the 
  1172. program. The following relational and equality operators are valid in an <b>if/goto 
  1173. </b>statement: <b><</b>, <b>></b>, <b><=</b>, <b>>=</b>, <b>==</b>, or <b>!=</b>. The precedence of these operators is the same 
  1174. as in C++.<br>
  1175. Let us now consider several programs that demonstrate Simple's features. The 
  1176. first program (<a href="^Code::c:s0p7"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.21</a>) reads two integers from the keyboard, stores the 
  1177. values in variables <b>a</b> and <b>b</b>, and computes and prints their sum (stored in variable 
  1178. <b>c</b>).<br>
  1179.  
  1180. </page>
  1181. <page pagename="Exercise 15.26">
  1182. The program of <a href="^Code::c:s0p6"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.22</a> determines and prints the larger of two integers. The 
  1183. integers are input from the keyboard and stored in <b>s</b> and <b>t</b>. The <b>if/goto</b> statement 
  1184. tests the condition <b>s >= t</b>. If the condition is true, control is transferred to line <b>90</b> 
  1185. and <b>s</b> is output; otherwise, <b>t</b> is output and control is transferred to the <b>end</b> 
  1186. statement in line <b>99</b> where the program terminates. <br>
  1187. Simple does not provide a repetition structure (such as C++'s for, <b>while</b>, or <b>do/
  1188. while</b>). However, Simple can simulate each of C++'s repetition structures using 
  1189. the <b>if/goto</b> and <b>goto</b> statements. <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 15.23</a> uses a sentinel-controlled loop to 
  1190. calculate the squares of several integers. Each integer is input from the keyboard 
  1191. and stored in variable <b>j</b>. If the value entered is the sentinel <b>-9999</b>, control is 
  1192. transferred to line <b>99</b> where the program terminates. Otherwise, <b>k</b> is assigned the <br>
  1193.  
  1194. </page>
  1195. <page pagename="Exercise 15.26">
  1196. square of <b>j</b>, <b>k</b> is output to the screen, and control is passed to line <b>20</b> where the 
  1197. next integer is input.<br>
  1198. Using the sample programs of <a href="^Code::c:s0p7"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.21</a>,<a href="^Code::c:s0p6"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.22</a>, and <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.23</a> as your 
  1199. guide, write a Simple program to accomplish each of the following:<br>
  1200. a)  Input three integers, determine their average, and print the result.<br>
  1201. b)  Use a sentinel-controlled loop to input 10 integers and compute and print 
  1202. their sum.<br>
  1203. c)  Use a counter-controlled loop to input 7 integers, some positive and some 
  1204. negative, and compute and print their average.<br>
  1205. d)  Input a series of integers and determine and print the largest. The first integer 
  1206. input indicates how many numbers should be processed.<br>
  1207. e)  Input 10 integers and print the smallest.<br>
  1208.  
  1209. </page>
  1210. <page pagename="Exercise 15.26">
  1211. f)  Calculate and print the sum of the even integers from 2 to 30.<br>
  1212. g)  Calculate and print the product of the odd integers from 1 to 9.<br>
  1213. <br>
  1214. <br>
  1215.  
  1216. </page>
  1217. <page pagename="Exercise 15.27">
  1218. <b>Exercise 15.27</b><br>
  1219. (<i>Building A Compiler; Prerequisite: Complete Exercises 5.18, 5.19,</i> <a href="^Exercises::c:s0p13"><img src="bckgrnds/icons/exercise.gif" align=sidebar>15.12</a>, 
  1220. <a href="^Exercises::c:s0p19"><img src="bckgrnds/icons/exercise.gif" align=sidebar>15.13</a>, and <a href="^Exercises::c:s0p47"><img src="bckgrnds/icons/exercise.gif" align=sidebar>15.26</a>) Now that the Simple language has been presented (<a href="^Exercises::c:s0p47"><img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 
  1221. 15.26</a>), we discuss how to build a Simple compiler. First, we consider the 
  1222. process by which a Simple program is converted to SML and executed by the 
  1223. Simpletron simulator (see <a href="^Illustration::c:s0p13"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.24</a>). A file containing a Simple program is 
  1224. read by the compiler and converted to SML code. The SML code is output to a 
  1225. file on disk, in which SML instructions appear one per line. The SML file is then 
  1226. loaded into the Simpletron simulator, and the results are sent to a file on disk and 
  1227. to the screen. Note that the Simpletron program developed in Exercise 5.19 took 
  1228. its input from the keyboard. It must be modified to read from a file so it can run 
  1229. the programs produced by our compiler.<br>
  1230.  
  1231. </page>
  1232. <page pagename="Exercise 15.27">
  1233. The Simple compiler performs two <i>passes</i> of the Simple program to convert it to 
  1234. SML. The first pass constructs a <i>symbol table</i> (object) in which every <i>line 
  1235. number</i> (object), <i>variable name</i> (object) and <i>constant</i> (object) of the Simple 
  1236. program is stored with its type and corresponding location in the final SML code 
  1237. (the symbol table is discussed in detail below). The first pass also produces the 
  1238. corresponding SML instruction object(s) for each of the Simple statements 
  1239. (object, etc.). As we will see, if the Simple program contains statements that 
  1240. transfer control to a line later in the program, the first pass results in an SML 
  1241. program containing some "unfinished" instructions. The second pass of the 
  1242. compiler locates and completes the unfinished instructions, and outputs the SML 
  1243. program to a file.<br>
  1244.  
  1245. </page>
  1246. <page pagename="Exercise 15.27">
  1247. <font size=18><i>First Pass 
  1248. </i></font><br>
  1249. The compiler begins by reading one statement of the Simple program into 
  1250. memory. The line must be separated into its individual <i>tokens</i> (i.e., "pieces" of a 
  1251. statement) for processing and compilation (standard library function <b>strtok</b> can 
  1252. be used to facilitate this task). Recall that every statement begins with a line 
  1253. number followed by a command. As the compiler breaks a statement into 
  1254. tokens, if the token is a line number, a variable, or a constant, it is placed in the 
  1255. symbol table. A line number is placed in the symbol table only if it is the first 
  1256. token in a statement. The <b>symbolTable</b> object is an array of <b>tableEntry</b> objects 
  1257. representing each symbol in the program. There is no restriction on the number 
  1258. of symbols that can appear in the program. Therefore, the <b>symbolTable</b> for a <br>
  1259.  
  1260. </page>
  1261. <page pagename="Exercise 15.27">
  1262. particular program could be large. Make the <b>symbolTable</b> a 100-element array 
  1263. for now. You can increase or decrease its size once the program is working.<br>
  1264. Each <b>tableEntry</b> object contains three members. Member <b>symbol</b> is an integer 
  1265. containing the ASCII representation of a variable (remember that variable 
  1266. names are single characters), a line number, or a constant. Member <b>type</b> is one 
  1267. of the following characters indicating the symbol's type: '<b>C</b>' for constant, '<b>L</b>' 
  1268. for line number, or '<b>V</b>' for variable. Member <b>location</b> contains the Simpletron 
  1269. memory location (00 to <b>99</b>) to which the symbol refers. Simpletron memory is 
  1270. an array of 100 integers in which SML instructions and data are stored. For a 
  1271. line number, the location is the element in the Simpletron memory array at 
  1272. which the SML instructions for the Simple statement begin. For a variable or 
  1273. constant, the location is the element in the Simpletron memory array in which <br>
  1274.  
  1275. </page>
  1276. <page pagename="Exercise 15.27">
  1277. the variable or constant is stored. Variables and constants are allocated from the 
  1278. end of Simpletron's memory backwards. The first variable or constant is stored 
  1279. in location at <b>99</b>, the next in location at <b>98</b>, etc. <br>
  1280. The symbol table plays an integral part in converting Simple programs to SML. 
  1281. We learned in Chapter 5 that an SML instruction is a four-digit integer 
  1282. comprised of two parts--the <i>operation code</i> and the <i>operand</i>. The operation 
  1283. code is determined by commands in Simple. For example, the simple command 
  1284. <b>input</b> corresponds to SML operation code <b>10</b> (read), and the Simple command 
  1285. <b>print</b> corresponds to SML operation code <b>11</b> (write). The operand is a memory 
  1286. location containing the data on which the operation code performs its task (e.g., 
  1287. operation code <b>10</b> reads a value from the keyboard and stores it in the memory 
  1288. location specified by the operand). The compiler searches <b>symbolTable</b> to <br>
  1289.  
  1290. </page>
  1291. <page pagename="Exercise 15.27">
  1292. determine the Simpletron memory location for each symbol so the 
  1293. corresponding location can be used to complete the SML instructions. <br>
  1294. The compilation of each Simple statement is based on its command. For 
  1295. example, after the line number in a <b>rem</b> statement is inserted in the symbol 
  1296. table, the remainder of the statement is ignored by the compiler because a 
  1297. remark is for documentation purposes only. The <b>input</b>, <b>print</b>, <b>goto</b> and <b>end</b> 
  1298. statements correspond to the SML <i>read</i>, <i>write</i>, <i>branch</i> (to a specific location) 
  1299. and <i>halt</i> instructions. Statements containing these Simple commands are 
  1300. converted directly to SML (note that a <b>goto</b> statement may contain an 
  1301. unresolved reference if the specified line number refers to a statement further 
  1302. into the Simple program file; this is sometimes called a forward reference). <br>
  1303.  
  1304. </page>
  1305. <page pagename="Exercise 15.27">
  1306. When a <b>goto</b> statement is compiled with an unresolved reference, the SML 
  1307. instruction must be <i>flagged</i> to indicate that the second pass of the compiler must 
  1308. complete the instruction. The flags are stored in 100-element array <b>flags</b> of type 
  1309. <b>int</b> in which each element is initialized to <b>-1</b>. If the memory location to which a 
  1310. line number in the Simple program refers is not yet known (i.e., it is not in the 
  1311. symbol table), the line number is stored in array <b>flags</b> in the element with the 
  1312. same subscript as the incomplete instruction. The operand of the incomplete 
  1313. instruction is set to <b>00</b> temporarily. For example, an unconditional branch 
  1314. instruction (making a forward reference) is left as <b>+4000</b> until the second pass of 
  1315. the compiler. The second pass of the compiler will be described shortly.<br>
  1316. Compilation of <b>if/goto</b> and <b>let</b> statements is more complicated than other 
  1317. statements--they are the only statements that produce more than one SML <br>
  1318.  
  1319. </page>
  1320. <page pagename="Exercise 15.27">
  1321. instruction. For an <b>if/goto</b> statement, the compiler produces code to test the 
  1322. condition and to branch to another line if necessary. The result of the branch 
  1323. could be an unresolved reference. Each of the relational and equality operators 
  1324. can be simulated using SML's <i>branch zero</i> and<i> branch negative</i> instructions (or 
  1325. possibly a combination of both). <br>
  1326. For a <b>let</b> statement, the compiler produces code to evaluate an arbitrarily 
  1327. complex arithmetic expression consisting of integer variables and/or constants. 
  1328. Expressions should separate each operand and operator with spaces.<a href="^Exercises::c:s0p13"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercises 
  1329. 15.12</a> and<a href="^Exercises::c:s0p19"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>15.13</a> presented the infix-to-postfix conversion algorithm and the 
  1330. postfix evaluation algorithm used by compilers to evaluate expressions. Before 
  1331. proceeding with your compiler, you should complete each of these exercises. <br>
  1332.  
  1333. </page>
  1334. <page pagename="Exercise 15.27">
  1335. When a compiler encounters an expression, it converts the expression from infix 
  1336. notation to postfix notation, then evaluates the postfix expression. <br>
  1337. How is it that the compiler produces the machine language to evaluate an 
  1338. expression containing variables? The postfix evaluation algorithm contains a 
  1339. "hook" where the compiler can generate SML instructions rather than actually 
  1340. evaluating the expression. To enable this "hook" in the compiler, the postfix 
  1341. evaluation algorithm must be modified to search the symbol table for each 
  1342. symbol it encounters (and possibly insert it), determine the symbol's 
  1343. corresponding memory location, and <i>push the memory location onto the stack</i> 
  1344. (<i>instead of the symbol</i>). When an operator is encountered in the postfix 
  1345. expression, the two memory locations at the top of the stack are popped and 
  1346. machine language for effecting the operation is produced using the memory <br>
  1347.  
  1348. </page>
  1349. <page pagename="Exercise 15.27">
  1350. locations as operands. The result of each subexpression is stored in a temporary 
  1351. location in memory and pushed back onto the stack so the evaluation of the 
  1352. postfix expression can continue. When postfix evaluation is complete, the 
  1353. memory location containing the result is the only location left on the stack. This 
  1354. is popped and SML instructions are generated to assign the result to the variable 
  1355. at the left of the let statement.<br>
  1356. <font size=18><i>Second Pass
  1357. </i></font><br>
  1358. The second pass of the compiler performs two tasks: resolve any unresolved 
  1359. references and output the SML code to a file. Resolution of references occurs as 
  1360. follows:<br>
  1361. a)  Search the <b>flags</b> array for an unresolved reference (i.e., an element with a 
  1362. value other than <b>-1</b>).<br>
  1363.  
  1364. </page>
  1365. <page pagename="Exercise 15.27">
  1366. b)  Locate the object in array <b>symbolTable</b> containing the symbol stored in the 
  1367. <b>flags</b> array (be sure that the type of the symbol is '<b>L</b>' for line number).<br>
  1368. c)  Insert the memory location from member <b>location</b> into the instruction with 
  1369. the unresolved reference (remember that an instruction containing an unresolved 
  1370. reference has operand <b>00</b>).<br>
  1371. d)  Repeat steps 1, 2, and 3 until the end of the <b>flags</b> array is reached.<br>
  1372. After the resolution process is complete, the entire array containing the SML 
  1373. code is output to a disk file with one SML instruction per line. This file can be 
  1374. read by the Simpletron for execution (after the simulator is modified to read its 
  1375. input from a file). Compiling your first Simple program into an SML file and 
  1376. then executing that file should give you a real sense of personal 
  1377. accomplishment. <br>
  1378.  
  1379. </page>
  1380. <page pagename="Exercise 15.27">
  1381. <font size=18>A Complete Example</font><br>
  1382. The following example illustrates a complete conversion of a Simple program to 
  1383. SML as it will be performed by the Simple compiler. Consider a Simple 
  1384. program that inputs an integer and sums the values from 1 to that integer. The 
  1385. program and the SML instructions produced by the first pass of the Simple 
  1386. compiler are illustrated in <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.25</a>. The symbol table constructed by the first 
  1387. pass is shown in <a href="^Illustration::c:s0p14"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.26</a>.<br>
  1388. Most Simple statements convert directly to single SML instructions. The 
  1389. exceptions in this program are remarks, the <b>if/goto </b>statement in line <b>20</b>, and the 
  1390. <b>let</b> statements. Remarks do not translate into machine language. However, the 
  1391. line number for a remark is placed in the symbol table in case the line number is 
  1392. referenced in a <b>goto</b> statement or an <b>if/goto</b> statement. Line <b>20</b> of the program <br>
  1393.  
  1394. </page>
  1395. <page pagename="Exercise 15.27">
  1396. specifies that if the condition <b>y == x</b> is true, program control is transferred to line 
  1397. <b>60</b>. Because line <b>60</b> appears later in the program, the first pass of the compiler 
  1398. has not as yet placed <b>60</b> in the symbol table (statement line numbers are placed 
  1399. in the symbol table only when they appear as the first token in a statement). 
  1400. Therefore, it is not possible at this time to determine the operand of the SML 
  1401. <i>branch zero</i> instruction at location <b>03</b> in the array of SML instructions. The 
  1402. compiler places <b>60</b> in location <b>03</b> of the <b>flags</b> array to indicate that the second 
  1403. pass completes this instruction. <br>
  1404. We must keep track of the next instruction location in the SML array because 
  1405. there is not a one-to-one correspondence between Simple statements and SML 
  1406. instructions. For example, the <b>if/goto</b> statement of line <b>20</b> compiles into three 
  1407. SML instructions. Each time an instruction is produced, we must increment the <br>
  1408.  
  1409. </page>
  1410. <page pagename="Exercise 15.27">
  1411. <i>instruction counter</i> to the next location in the SML array. Note that the size of 
  1412. Simpletron's memory could present a problem for Simple programs with many 
  1413. statements, variables and constants. It is conceivable that the compiler will run 
  1414. out of memory. To test for this case, your program should contain a <i>data counter</i> 
  1415. to keep track of the location at which the next variable or constant will be stored 
  1416. in the SML array. If the value of the instruction counter is larger than the value 
  1417. of the data counter, the SML array is full. In this case, the compilation process 
  1418. should terminate and the compiler should print an error message indicating that 
  1419. it ran out of memory during compilation. This serves to emphasize that although 
  1420. the programmer is freed from the burdens of managing memory by the compiler, 
  1421. the compiler itself must carefully determine the placement of instructions and <br>
  1422.  
  1423. </page>
  1424. <page pagename="Exercise 15.27">
  1425. data in memory, and must check for such errors as memory being exhausted 
  1426. during the compilation process.<br>
  1427. <font size=18>A Step-by-Step View of the Compilation Process</font><br>
  1428. Let us now walk through the compilation process for the Simple program in <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 
  1429. 15.25</a>. The compiler reads the first line of the program<br>
  1430. <font size=2><br></font><font size=11><pre>
  1431. 5 rem sum 1 to x<p>
  1432. </pre></font>
  1433. into memory. The first token in the statement (the line number) is determined 
  1434. using <b>strtok</b> (see Chapters 5 and 16 for a discussion of C++'s string manipulation 
  1435. functions). The token returned by <b>strtok</b> is converted to an integer using <b>atoi</b> so 
  1436. the symbol <b>5</b> can be located in the symbol table. If the symbol is not found, it is 
  1437. inserted in the symbol table. Since we are at the beginning of the program and 
  1438. this is the first line, no symbols are in the table yet. So, <b>5</b> is inserted into the <br>
  1439.  
  1440. </page>
  1441. <page pagename="Exercise 15.27">
  1442. symbol table as type <b>L</b> (line number) and assigned the first location in SML 
  1443. array (<b>00</b>). Although this line is a remark, a space in the symbol table is still 
  1444. allocated for the line number (in case it is referenced by a <b>goto</b> or an <b>if/goto</b>). No 
  1445. SML instruction is generated for a <b>rem</b> statement, so the instruction counter is 
  1446. not incremented. <br>
  1447. The statement <br>
  1448. <font size=2><br></font><font size=11><pre>
  1449. 10 input x<p>
  1450. </pre></font>
  1451. is tokenized next. The line number <b>10</b> is placed in the symbol table as type <b>L</b> and 
  1452. assigned the first location in the SML array (<b>00</b> because a remark began the 
  1453. program so the instruction counter is currently <b>00</b>). The command <b>input</b> 
  1454. indicates that the next token is a variable (only a variable can appear in an <b>input</b> 
  1455. statement). Because <b>input</b> corresponds directly to an SML operation code, the <br>
  1456.  
  1457. </page>
  1458. <page pagename="Exercise 15.27">
  1459. compiler simply has to determine the location of <b>x</b> in the SML array. Symbol <b>x</b> is 
  1460. not found in the symbol table. So, it is inserted into the symbol table as the 
  1461. ASCII representation of <b>x</b>, given type <b>V</b>, and assigned location <b>99</b> in the SML 
  1462. array (data storage begins at <b>99</b> and is allocated backwards). SML code can now 
  1463. be generated for this statement. Operation code <b>10</b> (the SML read operation 
  1464. code) is multiplied by 100, and the location of <b>x</b> (as determined in the symbol 
  1465. table) is added to complete the instruction. The instruction is then stored in the 
  1466. SML array at location <b>00</b>. The instruction counter is incremented by 1 because a 
  1467. single SML instruction was produced. <br>
  1468. The statement <br>
  1469. <font size=2><br></font><font size=11><pre>
  1470. 15 rem   check y == x<p>
  1471. </pre></font>
  1472.  
  1473. </page>
  1474. <page pagename="Exercise 15.27">
  1475. is tokenized next. The symbol table is searched for line number <b>15</b> (which is not 
  1476. found). The line number is inserted as type <b>L</b> and assigned the next location in 
  1477. the array, <b>01</b> (remember that <b>rem</b> statements do not produce code, so the 
  1478. instruction counter is not incremented). <br>
  1479. The statement <br>
  1480. <font size=2><br></font><font size=11><pre>
  1481. 20 if y == x goto 60<p>
  1482. </pre></font>
  1483. is tokenized next. Line number <b>20</b> is inserted in the symbol table and given type 
  1484. <b>L</b> with the next location in the SML array <b>01</b>. The command <b>if</b> indicates that a 
  1485. condition is to be evaluated. The variable <b>y</b> is not found in the symbol table, so it 
  1486. is inserted and given the type <b>V</b> and the SML location <b>98</b>. Next, SML 
  1487. instructions are generated to evaluate the condition. Since there is no direct 
  1488. equivalent in SML for the <b>if/goto</b>, it must be simulated by performing a <br>
  1489.  
  1490. </page>
  1491. <page pagename="Exercise 15.27">
  1492. calculation using <b>x</b> and <b>y</b> and branching based on the result. If <b>y</b> is equal to <b>x</b>, the 
  1493. result of subtracting <b>x</b> from <b>y</b> is zero, so the <i>branch zero</i> instruction can be used 
  1494. with the result of the calculation to simulate the <b>if/goto </b>statement. The first step 
  1495. requires that <b>y</b> be loaded (from SML location <b>98</b>) into the accumulator. This 
  1496. produces the instruction <b>01 +2098</b>. Next, <b>x</b> is subtracted from the accumulator. 
  1497. This produces the instruction <b>02 +3199</b>. The value in the accumulator may be 
  1498. zero, positive, or negative. Since the operator is <b>==</b>, we want to <i>branch zero</i>. 
  1499. First, the symbol table is searched for the branch location (<b>60</b> in this case), 
  1500. which is not found. So, <b>60</b> is placed in the <b>flags</b> array at location <b>03</b>, and the 
  1501. instruction <b>03 +4200</b> is generated (we cannot add the branch location because 
  1502. we have not assigned a location to line <b>60</b> in the SML array yet). The instruction 
  1503. counter is incremented to <b>04</b>.<br>
  1504.  
  1505. </page>
  1506. <page pagename="Exercise 15.27">
  1507. The compiler proceeds to the statement<br>
  1508. <font size=2><br></font><font size=11><pre>
  1509. 25 rem   increment y<p>
  1510. </pre></font>
  1511. The line number <b>25</b> is inserted in the symbol table as type <b>L</b> and assigned SML 
  1512. location <b>04</b>. The instruction counter is not incremented.<br>
  1513. When the statement<br>
  1514. <font size=2><br></font><font size=11><pre>
  1515. 30 let y = y + 1 <p>
  1516. </pre></font>
  1517. is tokenized, the line number <b>30</b> is inserted in the symbol table as type <b>L</b> and 
  1518. assigned SML location <b>04</b>. Command <b>let</b> indicates that the line is an assignment 
  1519. statement. First, all the symbols on the line are inserted in the symbol table (if 
  1520. they are not already there). The integer 1 is added to the symbol table as type <b>C</b> 
  1521. and assigned SML location <b>97</b>. Next, the right side of the assignment is 
  1522. converted from infix to postfix notation. Then the postfix expression (<b>y 1 +</b>) is <br>
  1523.  
  1524. </page>
  1525. <page pagename="Exercise 15.27">
  1526. evaluated. Symbol <b>y</b> is located in the symbol table and its corresponding 
  1527. memory location is pushed onto the stack. Symbol <b>1</b> is also located in the 
  1528. symbol table and its corresponding memory location is pushed onto the stack. 
  1529. When the operator <b>+</b> is encountered, the postfix evaluator pops the stack into the 
  1530. right operand of the operator and pops the stack again into the left operand of the 
  1531. operator, then produces the SML instructions<br>
  1532. <font size=2><br></font><font size=11><pre>
  1533. 04 +2098   <i>(load </i><tt>y</tt><i>)</i><p>
  1534. 05 +3097   <i>(add </i>1<i>)</i><p>
  1535. </pre></font>
  1536. The result of the expression is stored in a temporary location in memory (<b>96</b>) 
  1537. with instruction<br>
  1538. <font size=2><br></font><font size=11><pre>
  1539. 06 +2196   <i>(store temporary)</i><p>
  1540. </pre></font>
  1541.  
  1542. </page>
  1543. <page pagename="Exercise 15.27">
  1544. and the temporary location is pushed on the stack. Now that the expression has 
  1545. been evaluated, the result must be stored in <b>y</b> (i.e., the variable on the left side of 
  1546. <b>=</b>). So, the temporary location is loaded into the accumulator and the 
  1547. accumulator is stored in <b>y</b> with the instructions<br>
  1548. <font size=2><br></font><font size=11><pre>
  1549. 07 +2096   <i>(load temporary)</i><p>
  1550. 08 +2198   <i>(store </i>y<i>)</i><p>
  1551. </pre></font>
  1552. The reader will immediately notice that SML instructions appear to be 
  1553. redundant. We will discuss this issue shortly.<br>
  1554. When the statement<br>
  1555. <font size=2><br></font><font size=11><pre>
  1556. 35 rem   add y to total<p>
  1557. </pre></font>
  1558. is tokenized, line number <b>35</b> is inserted in the symbol table as type <b>L</b> and 
  1559. assigned location <b>09</b>. <br>
  1560.  
  1561. </page>
  1562. <page pagename="Exercise 15.27">
  1563. The statement<br>
  1564. <font size=2><br></font><font size=11><pre>
  1565. 40 let t = t + y<p>
  1566. </pre></font>
  1567. is similar to line <b>30</b>. The variable <b>t</b> is inserted in the symbol table as type <b>V</b> and 
  1568. assigned SML location <b>95</b>. The instructions follow the same logic and format as 
  1569. line <b>30</b>, and the instructions <b>09</b> <b>+2095</b>, <b>10 +3098</b>, <b>11 +2194</b>, <b>12 +2094</b>, and <b>13</b> 
  1570. <b>+2195</b> are generated. Note that the result of <b>t + y</b> is assigned to temporary 
  1571. location <b>94</b> before being assigned to <b>t (95)</b>. Once again, the reader will note that 
  1572. the instructions in memory locations <b>11</b> and <b>12</b> appear to be redundant. Again, 
  1573. we will discuss this shortly.<br>
  1574. The statement<br>
  1575. <font size=2><br></font><font size=11><pre>
  1576. 45 rem   loop y<p>
  1577. </pre></font>
  1578.  
  1579. </page>
  1580. <page pagename="Exercise 15.27">
  1581. is a remark, so line <b>45</b> is added to the symbol table as type <b>L</b> and assigned SML 
  1582. location <b>14</b>. <br>
  1583. The statement<br>
  1584. <font size=2><br></font><font size=11><pre>
  1585. 50 goto 20<p>
  1586. </pre></font>
  1587. transfers control to line <b>20</b>. Line number <b>50</b> is inserted in the symbol table as 
  1588. type <b>L</b> and assigned SML location <b>14</b>. The equivalent of <b>goto</b> in SML is the 
  1589. <i>unconditional branch</i> (<b>40</b>) instruction that transfers control to a specific SML 
  1590. location. The compiler searches the symbol table for line <b>20</b> and finds that it 
  1591. corresponds to SML location <b>01</b>. The operation code (<b>40</b>) is multiplied by 100 
  1592. and location <b>01</b> is added to it to produce the instruction <b>14 +4001</b>. <br>
  1593. The statement<br>
  1594. <font size=2><br></font><font size=11><pre>
  1595. 55 rem   output result<p>
  1596. </pre></font>
  1597.  
  1598. </page>
  1599. <page pagename="Exercise 15.27">
  1600. is a remark, so line <b>55</b> is inserted in the symbol table as type <b>L</b> and assigned 
  1601. SML location <b>15</b>. <br>
  1602. The statement<br>
  1603. <font size=2><br></font><font size=11><pre>
  1604. 60 print t<p>
  1605. </pre></font>
  1606. is an output statement. Line number <b>60</b> is inserted in the symbol table as type <b>L</b> 
  1607. and assigned SML location <b>15</b>. The equivalent of <b>print</b> in SML is operation 
  1608. code <b>11</b> (<i>write</i>). The location of <b>t</b> is determined from the symbol table and added 
  1609. to the result of the operation code multiplied by 100.<br>
  1610. The statement <br>
  1611. <font size=2><br></font><font size=11><pre>
  1612. 99 end<p>
  1613. </pre></font>
  1614. is the final line of the program. Line number <b>99</b> is stored in the symbol table as 
  1615. type <b>L</b> and assigned SML location <b>16</b>. The <b>end</b> command produces the SML <br>
  1616.  
  1617. </page>
  1618. <page pagename="Exercise 15.27">
  1619. instruction <b>+4300</b> (<b>43</b> is <i>halt</i> in SML) which is written as the final instruction in 
  1620. the SML memory array.<br>
  1621. This completes the first pass of the compiler. We now consider the second pass. 
  1622. The <b>flags</b> array is searched for values other than <b>-1</b>. Location <b>03</b> contains <b>60</b>, so 
  1623. the compiler knows that instruction <b>03</b> is incomplete. The compiler completes 
  1624. the instruction by searching the symbol table for <b>60</b>, determining its location, 
  1625. and adding the location to the incomplete instruction. In this case, the search 
  1626. determines that line <b>60</b> corresponds to SML location <b>15</b>, so the completed 
  1627. instruction <b>03 +4215</b> is produced replacing <b>03 +4200</b>. The Simple program has 
  1628. now been compiled successfully.<br>
  1629. To build the compiler, you will have to perform each of the following tasks:<br>
  1630.  
  1631. </page>
  1632. <page pagename="Exercise 15.27">
  1633. a)  Modify the Simpletron simulator program you wrote in Exercise 5.19 to take 
  1634. its input from a file specified by the user (see Chapter 14). The simulator should 
  1635. output its results to a disk file in the same format as the screen output. Convert 
  1636. the simulator to be an object-oriented program. In particular, make each part of 
  1637. the hardware an object. Arrange the instruction types into a class hierarchy using 
  1638. inheritance. Then execute the program polymorphically simply by telling each 
  1639. instruction to execute itself with an <b>executeInstruction</b> message.<br>
  1640. b)  Modify the infix-to-postfix conversion algorithm of<a href="^Exercises::c:s0p13"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.12</a> to 
  1641. process multi-digit integer operands and single-letter variable name operands. 
  1642. Hint: Standard library function <b>strtok</b> can be used to locate each constant and 
  1643. variable in an expression, and constants can be converted from strings to 
  1644. integers using standard library function <b>atoi</b>. (Note: The data representation of <br>
  1645.  
  1646. </page>
  1647. <page pagename="Exercise 15.27">
  1648. the postfix expression must be altered to support variable names and integer 
  1649. constants.)<br>
  1650. c)  Modify the postfix evaluation algorithm to process multi-digit integer 
  1651. operands and variable name operands. Also, the algorithm should now 
  1652. implement the "hook" discussed above so that SML instructions are produced 
  1653. rather than directly evaluating the expression. Hint: Standard library function 
  1654. <b>strtok</b> can be used to locate each constant and variable in an expression, and 
  1655. constants can be converted from strings to integers using standard library 
  1656. function <b>atoi</b>. (Note: The data representation of the postfix expression must be 
  1657. altered to support variable names and integer constants.)<br>
  1658. d)  Build the compiler. Incorporate parts (b) and (c) for evaluating expressions in 
  1659. let statements. Your program should contain a function that performs the first <br>
  1660.  
  1661. </page>
  1662. <page pagename="Exercise 15.27">
  1663. pass of the compiler and a function that performs the second pass of the 
  1664. compiler. Both functions can call other functions to accomplish their tasks. 
  1665. Make your compiler as object oriented as possible.<br>
  1666. e)  Build the compiler. Incorporate parts (b) and (c) for evaluating expressions in 
  1667. <b>let</b> statements. Your program should contain a function that performs the first 
  1668. pass of the compiler and a function that performs the second pass of the 
  1669. compiler. Both functions can call other functions to accomplish their tasks. 
  1670. Make your compiler as object oriented as possible.<br>
  1671. <br>
  1672. <br>
  1673.  
  1674. </page>
  1675. <page pagename="Exercise 15.28">
  1676. <b>Exercise 15.28</b><br>
  1677. (<i>Optimizing the Simple Compiler</i>) When a program is compiled and converted 
  1678. into SML, a set of instructions is generated. Certain combinations of instructions 
  1679. often repeat themselves, usually in triplets called <i>productions</i>. A production 
  1680. normally consists of three instructions such as <i>load</i>, <i>add</i>, and <i>store</i>. For 
  1681. example, <a href="^Illustration::c:s0p19"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.27</a> illustrates five of the SML instructions that were produced 
  1682. in the compilation of the program in <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.25</a> The first three instructions are 
  1683. the production that adds <b>1</b> to <b>y</b>. Note that instructions <b>06</b> and <b>07</b> store the 
  1684. accumulator value in temporary location <b>96</b>, then load the value back into the 
  1685. accumulator so instruction <b>08</b> can store the value in location <b>98</b>. Often a 
  1686. production is followed by a load instruction for the same location that was just 
  1687. stored. This code can be <i>optimized</i> by eliminating the store instruction and the <br>
  1688.  
  1689. </page>
  1690. <page pagename="Exercise 15.28">
  1691. subsequent load instruction that operate on the same memory location, thus 
  1692. enabling the Simpletron to execute the program faster. <a href="^Illustration::c:s0p17"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.28</a> illustrates 
  1693. the optimized SML for the program of <a href="^Illustration::c:s0p15"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.25</a>. Note that there are four fewer 
  1694. instructions in the optimized code--a memory-space savings of 25%. <br>
  1695. Modify the compiler to provide an option for optimizing the Simpletron 
  1696. Machine Language code it produces. Manually compare the non-optimized code 
  1697. with the optimized code, and calculate the percentage reduction.<br>
  1698. <br>
  1699. <br>
  1700.  
  1701. </page>
  1702. <page pagename="Exercise 15.29">
  1703. <b>Exercise 15.29</b><br>
  1704. (<i>Modifications to the Simple compiler</i>) Perform the following modifications to 
  1705. the Simple compiler. Some of these modifications may also require 
  1706. modifications to the Simpletron Simulator program written in Exercise 5.19.<br>
  1707. a)  Allow the modulus operator (<b>%</b>) to be used in <b>let</b> statements. Simpletron 
  1708. Machine Language must be modified to include a modulus instruction. <br>
  1709. b)  Allow exponentiation in a <b>let</b> statement using <b>^</b> as the exponentiation 
  1710. operator. Simpletron Machine Language must be modified to include an 
  1711. exponentiation instruction. <br>
  1712. c)  Allow the compiler to recognize uppercase and lowercase letters in Simple 
  1713. statements (e.g., '<b>A</b>' is equivalent to '<b>a</b>'). No modifications to the Simpletron 
  1714. Simulator are required.<br>
  1715.  
  1716. </page>
  1717. <page pagename="Exercise 15.29">
  1718. d)  Allow <b>input</b> statements to read values for multiple variables such as <b>input x, 
  1719. y</b>. No modifications to the Simpletron Simulator are required.<br>
  1720. e)  Allow the compiler to output multiple values in a single <b>print</b> statement such 
  1721. as <b>print a, b, c</b>. No modifications to the Simpletron Simulator are required.<br>
  1722. f)  Add syntax-checking capabilities to the compiler so error messages are 
  1723. output when syntax errors are encountered in a Simple program. No 
  1724. modifications to the Simpletron Simulator are required.<br>
  1725. g)  Allow arrays of integers. No modifications to the Simpletron Simulator are 
  1726. required.<br>
  1727. h)  Allow subroutines specified by the Simple commands <b>gosub</b> and <b>return</b>. 
  1728. Command <b>gosub</b> passes program control to a subroutine and command <b>return</b> 
  1729. passes control back to the statement after the <b>gosub</b>. This is similar to a function <br>
  1730.  
  1731. </page>
  1732. <page pagename="Exercise 15.29">
  1733. call in C++. The same subroutine can be called from many <b>gosub</b> commands 
  1734. distributed throughout a program. No modifications to the Simpletron Simulator 
  1735. are required.<br>
  1736. i)  Allow repetition structures of the form <br>
  1737. <font size=2><br></font><font size=11><pre>
  1738. for x = 2 to 10 step 2<p>
  1739.    <i>Simple statements</i><p>
  1740. next<p>
  1741. </pre></font>
  1742. This for statement loops from <b>2</b> to <b>10</b> with an increment of <b>2</b>. The <b>next</b> line 
  1743. marks the end of the body of the <b>for</b> line. No modifications to the Simpletron 
  1744. Simulator are required.<br>
  1745. j)  Allow repetition structures of the form <br>
  1746. <font size=2><br></font><font size=11><pre>
  1747. for x = 2 to 10<p>
  1748. </pre></font>
  1749.  
  1750. </page>
  1751. <page pagename="Exercise 15.29">
  1752. <font size=2><br></font><font size=11><pre>
  1753.    <i>Simple statements
  1754. </i><p>
  1755. </pre></font>
  1756. <font size=2><br></font><font size=11><pre>
  1757. next<p>
  1758. </pre></font>
  1759. This <b>for</b> statement loops from <b>2</b> to <b>10</b> with a default increment of <b>1</b>. No 
  1760. modifications to the Simpletron Simulator are required.<br>
  1761. k)  Allow the compiler to process string input and output. This requires the 
  1762. Simpletron Simulator to be modified to process and store string values. Hint: 
  1763. Each Simpletron word can be divided into two groups, each holding a two-digit 
  1764. integer. Each two-digit integer represents the ASCII decimal equivalent of a 
  1765. character. Add a machine language instruction that will print a string beginning 
  1766. at a certain Simpletron memory location. The first half of the word at that 
  1767. location is a count of the number of characters in the string (i.e., the length of the 
  1768. string). Each succeeding half word contains one ASCII character expressed as <br>
  1769.  
  1770. </page>
  1771. <page pagename="Exercise 15.29">
  1772. two decimal digits. The machine language instruction checks the length and 
  1773. prints the string by translating each two-digit number into its equivalent 
  1774. character.<br>
  1775. l)  Allow the compiler to process floating-point values in addition to integers. 
  1776. The Simpletron Simulator must also be modified to process floating-point 
  1777. values.<br>
  1778. <br>
  1779. <br>
  1780.  
  1781. </page>
  1782. <page pagename="Exercise 15.30">
  1783. <b>Exercise 15.30</b><br>
  1784. (<i>A Simple interpreter</i>) An interpreter is a program that reads a high-level 
  1785. language program statement, determines the operation to be performed by the 
  1786. statement, and executes the operation immediately. The high-level language 
  1787. program is not converted into machine language first. Interpreters execute 
  1788. slowly because each statement encountered in the program must first be 
  1789. deciphered. If statements are contained in a loop, the statements are deciphered 
  1790. each time they are encountered in the loop. Early versions of the BASIC 
  1791. programming language were implemented as interpreters. <br>
  1792. Write an interpreter for the Simple language discussed in <a href="^Exercises::c:s0p47"><img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.26</a>. The 
  1793. program should use the infix-to-postfix converter developed in<a href="^Exercises::c:s0p13"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.12</a> 
  1794. and the postfix evaluator developed in <a href="^Exercises::c:s0p19"><img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.13</a> to evaluate expressions in <br>
  1795.  
  1796. </page>
  1797. <page pagename="Exercise 15.30">
  1798. a <b>let</b> statement. The same restrictions placed on the Simple language in<a href="^Exercises::c:s0p47"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 
  1799. 15.26</a> should be adhered to in this program. Test the interpreter with the Simple 
  1800. programs written in <a href="^Exercises::c:s0p47"><img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.26</a>. Compare the results of running these 
  1801. programs in the interpreter with the results of compiling the Simple programs 
  1802. and running them in the Simpletron Simulator built in Exercise 5.19. <br>
  1803. <br>
  1804. <br>
  1805.  
  1806. </page>
  1807. <page pagename="Exercise 15.31">
  1808. <b>Exercise 15.31</b><br>
  1809. (<i>Insert/Delete Anywhere in a Linked List</i>) Our linked list class template allowed 
  1810. insertions and deletions at only the front and the back of the linked list. These 
  1811. capabilities were convenient for us when we used private inheritance and 
  1812. composition to produce a stack class template and a queue class template with a 
  1813. minimal amount of code simply by reusing the list class template. Actually 
  1814. linked lists are more general that those we provided. Modify the linked list class 
  1815. template we developed in this chapter to handle insertions and deletions 
  1816. anywhere in the list.<br>
  1817. <br>
  1818. <br>
  1819.  
  1820. </page>
  1821. <page pagename="Exercise 15.32">
  1822. <b>Exercise 15.32</b><br>
  1823. (<i>List and Queues without Tail Pointers</i>) Our implementation of a linked list ( 
  1824. <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.3</a>) used both a <b>firstPtr</b> and a <b>lastPtr</b>. The <b>lastPtr</b> was useful for the 
  1825. <b>insertAtBack</b> and <b>removeFromBack</b> member functions of the <b>List</b> class. The 
  1826. <b>insertAtBack</b> function corresponds to the <b>enqueue</b> member function of the 
  1827. <b>Queue</b> class. Rewrite the <b>List</b> class so that it does not use a <b>lastPtr</b>. Thus, any 
  1828. operations on the tail of a list must begin searching the list from the front. Does 
  1829. this affect our implementation of the <b>Queue</b> class (<a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.12</a>)?<br>
  1830. <br>
  1831. <br>
  1832.  
  1833. </page>
  1834. <page pagename="Exercise 15.33">
  1835. <b>Exercise 15.33</b><br>
  1836. Use the composition version of the stack program (<a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.11</a>) to form a 
  1837. complete working stack program. Modify this program to <b>inline</b> the member 
  1838. functions. Compare the two approaches. Summarize the advantages and 
  1839. disadvantages of inlining member functions.<br>
  1840. <br>
  1841. <br>
  1842.  
  1843. </page>
  1844. <page pagename="Exercise 15.34">
  1845. <b>Exercise 15.34</b><br>
  1846. (<i>Performance of Binary Tree Sorting and Searching</i>) One problem with the 
  1847. binary tree sort is that the order in which the data is inserted affects the shape of 
  1848. the tree--for the same collection of data, different orderings can yield binary 
  1849. trees of dramatically different shapes. The performance of the binary tree sorting 
  1850. and searching algorithms is sensitive to the shape of the binary tree. What shape 
  1851. would a binary tree have if its data were inserted in increasing order? in 
  1852. decreasing order? What shape should the tree have to achieve maximal 
  1853. searching performance? <br>
  1854. <br>
  1855. <br>
  1856.  
  1857. </page>
  1858. <page pagename="Exercise 15.35">
  1859. <b>Exercise 15.35</b><br>
  1860. (<i>Indexed Lists</i>) As presented in the text, linked lists must be searched 
  1861. sequentially. For large lists, this can result in poor performance. A common 
  1862. technique for improving list searching performance is to create and maintain an 
  1863. index to the list. An index is a set of pointers to various key places in the list. For 
  1864. example, an application that searches a large list of names could improve 
  1865. performance by creating an index with 26 entries--one for each letter of the 
  1866. alphabet. A search operation for a last name beginning with 'Y' would then first 
  1867. search the index to determine where the 'Y' entries begin, and then "jump into" 
  1868. the list at that point and search linearly until the desired name is found. This 
  1869. would be much faster than searching the linked list from the beginning. Use the 
  1870. <b>List</b> class of <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.3</a> as the basis of an <b>IndexedList</b> class. Write a program that <br>
  1871.  
  1872. </page>
  1873. <page pagename="Exercise 15.35">
  1874. demonstrates the operation of indexed lists. Be sure to include member functions 
  1875. <b>insertInIndexedList</b>, <b>searchIndexedList</b>, and <b>deleteFromIndexedList</b>.<br>
  1876. <br>
  1877. <br>
  1878.  
  1879. </page>
  1880. </section>
  1881. <section type=Popup name=Code title="Code Examples">
  1882. <page>
  1883. <font size=18>Figure 15.3  Manipulating a linked list.</font><br>
  1884. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1885. <param  name="file" value="code/ch15/fig15_03.txt">
  1886. </applet><br>
  1887. <br>
  1888. <foreign  name="copy2disk" url="!jcpy Source/fig15_03.jar">
  1889. <foreign  name="audio" url="~audio/Ch15/15fig003.au">
  1890. <foreign  name="execute" url="!jarexe Run/fig15_03.jar">
  1891. <br>
  1892.  
  1893. </page>
  1894. <page>
  1895. <font size=18>Figure 15.9  A simple stack program.</font><br>
  1896. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1897. <param  name="file" value="code/ch15/fig15_09.txt">
  1898. </applet><br>
  1899. <br>
  1900. <foreign  name="copy2disk" url="!jcpy Source/fig15_09.jar">
  1901. <foreign  name="audio" url="~audio/Ch15/15fig009.au">
  1902. <foreign  name="execute" url="!jarexe Run/fig15_09.jar">
  1903. <br>
  1904.  
  1905. </page>
  1906. <page>
  1907. <font size=18>Figure 15.11  A simple stack program using composition.</font><br>
  1908. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1909. <param  name="file" value="code/ch15/fig15_11.txt">
  1910. </applet><br>
  1911. <br>
  1912. <foreign  name="copy2disk" url="!jcpy Source/fig15_11.jar">
  1913. <foreign  name="audio" url="~audio/Ch15/15fig011.au">
  1914. <foreign  name="execute" url="!jarexe Run/fig15_11.jar">
  1915. <br>
  1916.  
  1917. </page>
  1918. <page>
  1919. <font size=18>Figure 15.12  Processing a queue.</font><br>
  1920. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1921. <param  name="file" value="code/ch15/fig15_12.txt">
  1922. </applet><br>
  1923. <br>
  1924. <foreign  name="copy2disk" url="!jcpy Source/fig15_12.jar">
  1925. <foreign  name="audio" url="~audio/Ch15/15fig012.au">
  1926. <foreign  name="execute" url="!jarexe Run/fig15_12.jar">
  1927. <br>
  1928.  
  1929. </page>
  1930. <page>
  1931. <font size=18>Figure 15.16  Creating and traversing a binary tree.</font><br>
  1932. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1933. <param  name="file" value="code/ch15/fig15_16.txt">
  1934. </applet><br>
  1935. <br>
  1936. <foreign  name="copy2disk" url="!jcpy Source/fig15_16.jar">
  1937. <foreign  name="audio" url="~audio/Ch15/15fig016.au">
  1938. <foreign  name="execute" url="!jarexe Run/fig15_16.jar">
  1939. <br>
  1940.  
  1941. </page>
  1942. <page>
  1943. <font size=18>Figure 15.23  Calculate the squares of several integers.</font><br>
  1944. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1945. <param  name="file" value="code/ch15/fig15_23.txt">
  1946. </applet><br>
  1947. <br>
  1948.  
  1949. </page>
  1950. <page>
  1951. <font size=18>Figure 15.22  Simple program that finds the larger of two integers.</font><br>
  1952. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1953. <param  name="file" value="code/ch15/fig15_22.txt">
  1954. </applet><br>
  1955. <br>
  1956.  
  1957. </page>
  1958. <page>
  1959. <font size=18>Figure 15.21  Simple program that determines the sum of two integers.</font><br>
  1960. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1961. <param  name="file" value="code/ch15/fig15_16.txt">
  1962. </applet><br>
  1963. <br>
  1964.  
  1965. </page>
  1966. </section>
  1967. <section type=Popup name=Perform title="Performance">
  1968. <page>
  1969. Using dynamic 
  1970. memory allocation 
  1971. (instead of arrays) for 
  1972. data structures that 
  1973. grow and shrink at 
  1974. execution time can save 
  1975. memory. Keep in mind, 
  1976. however, that pointers 
  1977. occupy space, and that 
  1978. dynamic memory <br>
  1979.  
  1980. </page>
  1981. <page>
  1982. allocation incurs the 
  1983. overhead of function 
  1984. calls.<br>
  1985. <br>
  1986.  
  1987. </page>
  1988. <page>
  1989. The elements of an 
  1990. array are stored 
  1991. contiguously in 
  1992. memory. This allows 
  1993. immediate access to 
  1994. any array element 
  1995. because the address of 
  1996. any element can be 
  1997. calculated directly 
  1998. based on its position <br>
  1999.  
  2000. </page>
  2001. <page>
  2002. relative to the 
  2003. beginning of the array. 
  2004. Linked lists do not 
  2005. afford such immediate 
  2006. "direct access" to their 
  2007. elements.<br>
  2008. <br>
  2009.  
  2010. </page>
  2011. <page>
  2012. Insertion and deletion 
  2013. in a sorted array can be 
  2014. time consuming--all 
  2015. the elements following 
  2016. the inserted or deleted 
  2017. element must be shifted 
  2018. appropriately. <br>
  2019. <br>
  2020.  
  2021. </page>
  2022. <page>
  2023. An array can be 
  2024. declared to contain 
  2025. more elements than the 
  2026. number of items 
  2027. expected, but this can 
  2028. waste memory. Linked 
  2029. lists can provide better 
  2030. memory utilization in 
  2031. these situations. Linked <br>
  2032.  
  2033. </page>
  2034. <page>
  2035. lists allow the program 
  2036. to adapt at run time.<br>
  2037. <br>
  2038.  
  2039. </page>
  2040. </section>
  2041. <section type=Popup name=Practice title="Good Practices">
  2042. <page>
  2043. When memory that was 
  2044. dynamically allocated 
  2045. with <b>new</b> is no longer 
  2046. needed, use <b>delete</b> to 
  2047. return the memory to 
  2048. the system 
  2049. immediately. <br>
  2050. <br>
  2051.  
  2052. </page>
  2053. <page>
  2054. Assign null (zero) to 
  2055. the link member of a 
  2056. new node. Pointers 
  2057. should be initialized 
  2058. before they are used.<br>
  2059. <br>
  2060.  
  2061. </page>
  2062. </section>
  2063. <section type=Popup name=Objective title="Objectives">
  2064. <page>
  2065. <indent width=8 delay>*   To be able to form linked data structures using pointers, self-referential 
  2066. classes, and recursion.</indent>
  2067. <indent width=8 delay>*   To be able to create and manipulate dynamic data structures such as 
  2068. linked lists, queues, stacks, and binary trees.</indent>
  2069. <indent width=8 delay>*   To understand various important applications of linked data structures.</indent>
  2070. <foreign  name="audio" url="~audio/Ch15/15obj.au">
  2071.  
  2072. </page>
  2073. <page>
  2074. <indent width=8 delay>*   To understand how to create reusable data structures with class templates, 
  2075. inheritance, and composition.</indent>
  2076. <br>
  2077.  
  2078. </page>
  2079. </section>
  2080. <section type=Popup name=Errors title="Common Errors">
  2081. <page>
  2082. Not setting the link in 
  2083. the last node of a list to 
  2084. null (<b>0</b>).<br>
  2085. <br>
  2086.  
  2087. </page>
  2088. <page>
  2089. Assuming that the size 
  2090. of a class object is 
  2091. simply the sum of the 
  2092. sizes of its data 
  2093. members.<br>
  2094.  
  2095. </page>
  2096. <page>
  2097. Deleting memory with 
  2098. <b>delete</b> that was not 
  2099. allocated dynamically 
  2100. with <b>new</b>.<br>
  2101. <br>
  2102.  
  2103. </page>
  2104. <page>
  2105. Not returning 
  2106. dynamically allocated 
  2107. memory when it is no 
  2108. longer needed can 
  2109. cause the system to run 
  2110. out of memory 
  2111. prematurely. This is 
  2112. sometimes called a 
  2113. "memory leak."<br>
  2114. <br>
  2115.  
  2116. </page>
  2117. <page>
  2118. Trying to delete 
  2119. memory that has 
  2120. already been deleted 
  2121. can lead to 
  2122. unpredictable results at 
  2123. execution time.<br>
  2124. <br>
  2125.  
  2126. </page>
  2127. <page>
  2128. Referring to memory 
  2129. that has been deleted.<br>
  2130. <br>
  2131.  
  2132. </page>
  2133. <page>
  2134. Not setting the link in 
  2135. the bottom node of a 
  2136. stack to null (zero).<br>
  2137. <br>
  2138.  
  2139. </page>
  2140. <page>
  2141. Not setting the link in 
  2142. the last node of a queue 
  2143. to null (zero).<br>
  2144. <br>
  2145.  
  2146. </page>
  2147. <page>
  2148. Not setting to null 
  2149. (zero) the links in leaf 
  2150. nodes of a tree.<br>
  2151. <br>
  2152.  
  2153. </page>
  2154. </section>
  2155. <section type=Body name=Default title="15 Data Structures">
  2156. <page>
  2157. <font size=18 bold>15 Data Structures</font><hr>
  2158. <a href="#s1p0">15.1<spacer width=20 height=1>Introduction</a>  <br>
  2159. <a href="#s2p0">15.2<spacer width=20 height=1>Self-Referential Classes</a>  <br>
  2160. <a href="#s3p0">15.3<spacer width=20 height=1>Dynamic Memory Allocation</a>  <br>
  2161. <a href="#s4p0">15.4<spacer width=20 height=1>Linked Lists</a>  <br>
  2162. <a href="#s5p0">15.5<spacer width=20 height=1>Stacks</a>  <br>
  2163. <a href="#s6p0">15.6<spacer width=20 height=1>Queues</a>  <br>
  2164. <a href="#s7p0">15.7<spacer width=20 height=1>Trees</a>  <br>
  2165. <a href="#s8p0">15.8<spacer width=20 height=1>Summary</a>  <br>
  2166. <a href="^Terminology::c:s0p0">Terminology</a>  <br>
  2167. <a href="^Illustration::c:s0p0">Figures</a>  <br>
  2168. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  2169. <foreign  name="quotes" url="^Quotes::c:s0p0">
  2170.  
  2171. </page>
  2172. </section>
  2173. <section type=Body name=Default title="15.1 Introduction">
  2174. <page>
  2175. <font size=18 bold>15.1 Introduction</font><hr>
  2176. We have studied fixed-size <i>data structures</i> such as 
  2177. single-subscripted arrays, double-subscripted arrays 
  2178. and <b>struct</b>s. This chapter introduces <i>dynamic data 
  2179. structures</i> that grow and shrink during execution. 
  2180. <i>Linked lists</i> are collections of data items "lined up in a 
  2181. row"--insertions and removals are made anywhere in a 
  2182. linked list. <i>Stacks</i> are important in compilers and 
  2183. operating systems--insertions and removals are made 
  2184. only at one end of a stack--its <i>top</i>. <i>Queues</i> represent 
  2185. waiting lines; insertions are made at the back (also 
  2186. referred to as the <i>tail</i>) of a queue, and removals are <br>
  2187.  
  2188. </page>
  2189. <page>
  2190. made from the front (also referred to as the <i>head</i>) of a 
  2191. queue. <i>Binary trees</i> facilitate high-speed searching and 
  2192. sorting of data, efficient elimination of duplicate data 
  2193. items, representing file system directories, and 
  2194. compiling expressions into machine language. These 
  2195. data structures have many other interesting 
  2196. applications.<br>
  2197. <spacer width=16 height=1>We will discuss the major types of data structures and 
  2198. implement programs that create and manipulate these 
  2199. data structures. We use classes, class templates, 
  2200. inheritance and composition to create and package 
  2201. these data structures for reusability and maintainability.<br>
  2202.  
  2203. </page>
  2204. <page>
  2205. Studying this chapter is solid preparation for Chapter 
  2206. 20, "Standard Template Library (STL)." The STL is a 
  2207. major portion of the C++ Standard Library. The STL 
  2208. provides containers, iterators for traversing those 
  2209. containers and algorithms for processing the elements 
  2210. of those containers. You will see that the STL has taken 
  2211. each of the data structures we discuss here in Chapter 
  2212. 15 and packaged them into templatized classes. The 
  2213. STL code is carefully written to be portable, efficient 
  2214. and extensible. Once you understand the principles and 
  2215. construction of data structures as presented in Chapter 
  2216. 15, you will be able to make the best use of the 
  2217. prepackaged data structures, iterators and algorithms in <br>
  2218.  
  2219. </page>
  2220. <page>
  2221. the STL. STL is by far the single most important 
  2222. enhancement to C++ in the ANSI/ISO Draft Standard. 
  2223. It is a world-class set of components for helping realize 
  2224. the vision of reuse, reuse, reuse.<br>
  2225. <spacer width=16 height=1>The chapter examples are practical programs that you 
  2226. will be able to use in more advanced courses and in 
  2227. industry applications. The programs are especially 
  2228. heavy on pointer manipulation. The exercises include a 
  2229. rich collection of useful applications. <br>
  2230. <spacer width=16 height=1>We encourage you to attempt the major project 
  2231. described in the special section entitled "Building Your 
  2232. Own Compiler." You have been using a compiler to 
  2233. translate your C++ programs to machine language so <br>
  2234.  
  2235. </page>
  2236. <page>
  2237. that you could execute these programs on your 
  2238. computer. In this project, you will actually build your 
  2239. own compiler. It will read a file of statements written in 
  2240. a simple, yet powerful, high-level language similar to 
  2241. early versions of the popular language BASIC. Your 
  2242. compiler will translate these statements into a file of 
  2243. Simpletron Machine Language (SML) instructions--
  2244. SML is the language you learned in the Chapter 5 
  2245. special section, "Building Your Own Computer." Your 
  2246. Simpletron Simulator program will then execute the 
  2247. SML program produced by your compiler! 
  2248. Implementing this project using a heavily object-
  2249. oriented approach will give you a wonderful <br>
  2250.  
  2251. </page>
  2252. <page>
  2253. opportunity to exercise most of what you have learned 
  2254. in this course. The special section carefully walks you 
  2255. through the specifications of the high-level language, 
  2256. and describes the algorithms you will need to convert 
  2257. each type of high-level language statement into 
  2258. machine language instructions. If you enjoy being 
  2259. challenged, you might attempt the many enhancements 
  2260. to both the compiler and the Simpletron Simulator 
  2261. suggested in the Exercises.<br>
  2262.  
  2263. </page>
  2264. </section>
  2265. <section type=Body name=Default title="15.2 Self-Referential Classes">
  2266. <page>
  2267. <font size=18 bold>15.2 Self-Referential Classes</font><hr>
  2268. A <i>self-referential class</i> contains a pointer member that 
  2269. points to a class object of the same class type. For 
  2270. example, the definition<br>
  2271. <font size=2><br></font><font size=11><pre>
  2272. class Node {  <p>
  2273. public:<p>
  2274.    Node( int );<p>
  2275.    void setData( int );<p>
  2276.    int getData() const;      <p>
  2277.    void setNextPtr( const Node * );<p>
  2278.    const Node *getNextPtr() const;<p>
  2279. private:<p>
  2280.    int data;<p>
  2281.    Node *nextPtr;<p>
  2282. };<p>
  2283. </pre></font>
  2284.  
  2285. </page>
  2286. <page>
  2287. defines a type, <b>Node</b>. Type <b>Node</b> has two private data 
  2288. members--integer member <b>data</b> and pointer member 
  2289. <b>nextPtr</b>. Member <b>nextPtr</b> points to an object of type 
  2290. <b>Node</b>--an object of the same type as the one being 
  2291. declared here, hence the term "self-referential class." 
  2292. Member <b>nextPtr</b> is referred to as a <i>link</i>--i.e., <b>nextPtr</b> 
  2293. can be used to "tie" an object of type <b>Node</b> to another 
  2294. object of the same type. Type <b>Node</b> also has five 
  2295. member functions: a constructor that receives an integer 
  2296. to initialize member <b>data</b>, a <b>setData</b> function to set the 
  2297. value of member <b>data</b>, a <b>getData</b> function to return the 
  2298. value of member <b>data</b>, a <b>setNextPtr</b> function to set the <br>
  2299.  
  2300. </page>
  2301. <page>
  2302. value of member <b>nextPtr</b>, and a <b>getNextPtr</b> function to 
  2303. return the value of member <b>nextPtr</b>.<br>
  2304. <spacer width=16 height=1>Self-referential class objects can be linked together to 
  2305. form useful data structures such as lists, queues, stacks, 
  2306. and trees. <a href="^Illustration::c:s0p2"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.1</a> illustrates two self-referential 
  2307. class objects linked together to form a list. Note that a 
  2308. slash--representing a null (<b>0</b>) pointer--is placed in the 
  2309. link member of the second self-referential class object 
  2310. to indicate that the link does not point to another object. 
  2311. The slash is only for illustration purposes; it does not 
  2312. correspond to the backslash character in C++. <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>A null 
  2313. pointer normally indicates the end of a data structure <br>
  2314.  
  2315. </page>
  2316. <page>
  2317. just as the null character ('<b>\\0</b>') indicates the end of a 
  2318. string.<br>
  2319.  
  2320. </page>
  2321. <page>
  2322. <b>Select the true statement(s). </b><br>
  2323. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A self-referential class contains a pointer to an object of the same class type.">
  2324. A self-referential class contains an object of the same class type.   <br>
  2325. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2326. Self-referential class objects can be linked together to form data structures such as lists, queues, stacks, and trees. <br>
  2327. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2328.  
  2329. </page>
  2330. </section>
  2331. <section type=Body name=Default title="15.3 Dynamic Memory Allocation">
  2332. <page>
  2333. <font size=18 bold>15.3 Dynamic Memory Allocation</font><hr>
  2334. Creating and maintaining dynamic data structures 
  2335. requires <i>dynamic memory allocation</i>--the ability for a 
  2336. program to obtain more memory space at execution 
  2337. time to hold new nodes and to release space no longer 
  2338. needed. The limit for dynamic memory allocation can 
  2339. be as large as the amount of available physical memory 
  2340. in the computer or the amount of available virtual 
  2341. <a href="^Errors::c:s0p1"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>memory in a virtual memory system. Often, the limits 
  2342. are much smaller because available memory must be 
  2343. shared among many users.<br>
  2344.  
  2345. </page>
  2346. <page>
  2347. <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Operators <b>new</b> and <b>delete</b> are essential to dynamic 
  2348. memory allocation. Operator <b>new</b> takes as an argument 
  2349. the type of the object being dynamically allocated and 
  2350. returns a pointer to an object of that type. For example, 
  2351. the statement<br>
  2352. <font size=2><br></font><font size=11><pre>
  2353. Node *newPtr = new Node( 10 );<p>
  2354. </pre></font>
  2355. allocates <b>sizeof( Node )</b> <b><a href="^Portable::c:s0p0"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a></b>bytes and stores a pointer to 
  2356. this memory in <b>newPtr</b>. If no memory is available, <b>new</b> 
  2357. returns a zero pointer. The 10 is the node object's data.<br>
  2358. <spacer width=16 height=1><a href="^Errors::c:s0p3"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>The <b>delete</b> operator deallocates memory allocated with 
  2359. <b>new</b>--i.e., the memory is returned to the system so that 
  2360. the memory can be reallocated in the future. <a href="^Errors::c:s0p2"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>To free <br>
  2361.  
  2362. </page>
  2363. <page>
  2364. memory dynamically allocated by the preceding <b>new</b>, 
  2365. use the statement<br>
  2366. <font size=2><br></font><font size=11><pre>
  2367. delete newPtr;<p>
  2368. </pre></font>
  2369. <a href="^Errors::c:s0p5"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Note that <b>newPtr</b> itself is not deleted; rather the space 
  2370. <b>newPtr</b> points to is deleted. If <b>newPtr</b> has the value <b>0</b> 
  2371. (indicating a pointer to nothing), the preceding 
  2372. <a href="^Errors::c:s0p4"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>statement has no effect.<br>
  2373. <spacer width=16 height=1>The following sections discuss lists, stacks, queues and 
  2374. trees. These data structures are created and maintained 
  2375. with dynamic memory allocation and self-referential 
  2376. classes. <br>
  2377.  
  2378. </page>
  2379. </section>
  2380. <section type=Body name=Default title="15.4 Linked Lists">
  2381. <page>
  2382. <font size=18 bold>15.4 Linked Lists</font><hr>
  2383. A <i>linked list</i> is a linear collection of self-referential 
  2384. class objects, called <i>nodes</i>, connected by pointer 
  2385. <i>links</i>--hence, the term "linked" list. A linked list is 
  2386. accessed via a pointer to the first node of the list. 
  2387. Subsequent nodes are accessed via the link-pointer 
  2388. member stored in each node. By convention, the link 
  2389. pointer in the last node of a list is set to null (zero) to 
  2390. mark the end of the list. Data are stored in a linked list 
  2391. dynamically--each node is created as necessary. A 
  2392. node can contain data of any type including objects of 
  2393. other classes. If nodes contain base-class pointers or <br>
  2394.  
  2395. </page>
  2396. <page>
  2397. base-class references to base-class and derived-class 
  2398. objects related by inheritance, we can have a linked list 
  2399. of such nodes and use <b>virtual</b> function calls to process 
  2400. these objects polymorphically. Stacks and queues are 
  2401. also linear data structures, and, as we will see, are 
  2402. constrained versions of linked lists. Trees are nonlinear 
  2403. data structures.<br>
  2404. <spacer width=16 height=1>Lists of data can be stored in arrays, but linked lists 
  2405. provide several advantages. A linked list is appropriate 
  2406. when the number of data elements to be represented in 
  2407. the data structure at once is unpredictable. Linked lists 
  2408. are dynamic, so the length of a list can increase or 
  2409. decrease as necessary. The size of a "conventional" <br>
  2410.  
  2411. </page>
  2412. <page>
  2413. C++ array, however, cannot be altered, because the 
  2414. array size is fixed at compile time. "Conventional" 
  2415. arrays can become full. <a href="^Perform::c:s0p5"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>Linked lists become full only 
  2416. when the system has insufficient memory to satisfy 
  2417. dynamic storage allocation requests. <br>
  2418. <spacer width=16 height=1><a href="^Perform::c:s0p4"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>Linked lists can be maintained in sorted order simply 
  2419. by inserting each new element at the proper point in the 
  2420. list. Existing list elements do not need to be moved.<br>
  2421. <spacer width=16 height=1>Linked list nodes are normally not stored <a href="^Perform::c:s0p2"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>contiguously 
  2422. in memory. Logically, however, the nodes of a linked 
  2423. <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>list appear to be contiguous. <a href="^Illustration::c:s0p3"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.2</a> illustrates a 
  2424. linked list with several nodes. <br>
  2425.  
  2426. </page>
  2427. <page>
  2428. The program of <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.3</a> (whose output is shown in 
  2429. Fig. 15.4) uses a <b>List</b> class template (see Chapter 12, 
  2430. "Templates") to manipulate a list of integer values and a 
  2431. list of floating-point values. The driver program 
  2432. (<a href="^Code::c:s0p0">f<img src="bckgrnds/icons/code_ico.gif" align=sidebar>ig15_03.cpp</a>) provides 5 options: 1) insert a value at 
  2433. the beginning of the list (function <b>insertAtFront</b>), 2) 
  2434. insert a value at the end of the list (function 
  2435. <b>insertAtBack</b>), 3) delete a value from the front of the 
  2436. list (function <b>removeFromFront</b>), 4) delete a value 
  2437. from the end of the list (function <b>removeFromBack</b>), 
  2438. and 5) terminate the list processing. A detailed 
  2439. discussion of the program follows.<a href="^Exercises::c:s0p31"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.20</a> asks 
  2440. you to implement a recursive function that prints a <br>
  2441.  
  2442. </page>
  2443. <page>
  2444. linked list backwards, and <a href="^Exercises::c:s0p32"><img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 15.21</a> asks you to 
  2445. implement a recursive function that searches a linked 
  2446. list for a particular data item.<br>
  2447. <spacer width=16 height=1><a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 15.3</a> consists of two class templates--<b>ListNode</b> 
  2448. and <b>List</b>. Encapsulated in each <b>List</b> object is a linked-
  2449. list of <b>ListNode</b> objects. The <b>ListNode</b> class template 
  2450. consists of private members <b>data</b> and <b>nextPtr</b>. 
  2451. <b>ListNode</b> member <b>data</b> stores a value of type 
  2452. <b>NODETYPE</b>, the type parameter passed to the class 
  2453. template. <b>ListNode</b> member <b>nextPtr</b> stores a pointer to 
  2454. the next <b>ListNode</b> object in the linked list.<br>
  2455. <spacer width=16 height=1>The <b>List</b> class template consists of <b>private</b> members 
  2456. <b>firstPtr</b> (a pointer to the first <b>ListNode</b> in a <b>List</b> object) <br>
  2457.  
  2458. </page>
  2459. <page>
  2460. and <b>lastPtr</b> (a pointer to the last <b>ListNode</b> in a <b>List</b> 
  2461. object). The default constructor initializes both pointers 
  2462. to <b>0</b> (null). The destructor ensures that all <b>ListNode</b> 
  2463. objects in a <b>List</b> object are destroyed when that <b>List</b> 
  2464. object is destroyed. The primary functions of the <b>List</b> 
  2465. class template are <b>insertAtFront</b>, <b>insertAtBack</b>, 
  2466. <b>removeFromFront</b>, and <b>removeFromBack</b>. <br>
  2467. <spacer width=16 height=1> <a href="^Practice::c:s0p1"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Function <b>isEmpty</b> is called a<i> predicate function</i>--it 
  2468. does not alter the <b>List</b>; rather, it determines if the <b>List</b> is 
  2469. empty (i.e., the pointer to the first node of the <b>List</b> is 
  2470. null). If the <b>List</b> is empty, <b>true</b> is returned; otherwise, 
  2471. <b>false</b> is returned. Function <b>print</b> displays the <b>List</b>'s 
  2472. contents.<br>
  2473.  
  2474. </page>
  2475. <page>
  2476. Over the next several pages, we will discuss each of the 
  2477. member functions of the <b>List</b> class in detail. Function 
  2478. <b>insertAtFront</b> (<a href="^Illustration::c:s0p4"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.5</a>) places a new node at the 
  2479. front of the list. The function consists of several steps:<br>
  2480. 1. Call function <b>getNewNode</b> passing it <b>value</b>, which is 
  2481. a constant reference to the node value to be inserted.<br>
  2482. 2. Function <b>getNewNode</b> uses operator <b>new</b> to create a 
  2483. new list node and return a pointer to this list node. If 
  2484. this pointer is <b>nonzero</b>, <b>getNewNode</b> returns a pointer 
  2485. to this newly allocated node to <b>newPtr</b> in <b>insertAtFront</b>.<br>
  2486. 3. If the list is empty, then both <b>firstPtr</b> and <b>lastPtr</b> are 
  2487. set to <b>newPtr</b>.<br>
  2488.  
  2489. </page>
  2490. <page>
  2491. 4. If the list is not empty, then the node pointed to by 
  2492. <b>newPtr</b> is threaded into the list by copying <b>firstPtr</b> to 
  2493. <b>newPtr->nextPtr</b> so that the new node points to what 
  2494. used to be the first node of the list, and copying <b>newPtr</b> 
  2495. to <b>firstPtr</b> so that <b>firstPtr</b> now points to the new first 
  2496. node of the list.<br>
  2497. <a href="^Illustration::c:s0p4"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.5</a> illustrates function <b>insertAtFront</b>. Part a) 
  2498. of the figure shows the list and the new node before the 
  2499. <b>insertAtFront</b> operation. The dotted arrows in part b) 
  2500. illustrate the steps 2 and 3 of the <b>insertAtFront</b> 
  2501. operation that enable the node containing 12 to become 
  2502. the new list front.<br>
  2503.  
  2504. </page>
  2505. <page>
  2506. Function <b>insertAtBack</b> (<a href="^Illustration::c:s0p5"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.6</a>) places a new node at 
  2507. the back of the list. The function consists of several 
  2508. steps:<br>
  2509. 1. Call function <b>getNewNode</b> passing it value which 
  2510. is a constant reference to the node value to be inserted.<br>
  2511. 2. Function <b>getNewNode</b> uses operator <b>new</b> to create a 
  2512. new list node and return a pointer to this list node. If 
  2513. this pointer is nonzero, <b>getNewNode</b> returns a pointer 
  2514. to this newly allocated node to <b>newPtr</b> in <b>insertAtBack</b>.<br>
  2515. 3. If the list is empty, then both <b>firstPtr</b> and <b>lastPtr</b> are 
  2516. set to <b>newPtr</b>.<br>
  2517.  
  2518. </page>
  2519. <page>
  2520. 4. If the list is not empty, then the node pointed to by 
  2521. <b>newPtr</b> is threaded into the list by copying <b>newPtr</b> into 
  2522. <b>lastPtr->nextPtr</b> so that the new node is pointed to by 
  2523. what used to be the last node of the list, and copying 
  2524. <b>newPtr</b> to <b>lastPtr</b> so that <b>lastPtr</b> now points to the new 
  2525. last node of the list.<br>
  2526. <a href="^Illustration::c:s0p5"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.6</a> illustrates an <b>insertAtBack</b> operation. Part 
  2527. a) of the figure shows the list and the new node before 
  2528. the operation. The dotted arrows in part b) illustrate the 
  2529. steps of function <b>insertAtBack</b> that enable a new node 
  2530. to be added to the end of a list that is not empty.<br>
  2531. <spacer width=16 height=1>Function <b>removeFromFront</b> (<a href="^Illustration::c:s0p6"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.7</a>) removes the 
  2532. front node of the list and copies the node value to the <br>
  2533.  
  2534. </page>
  2535. <page>
  2536. reference parameter. The function returns <b>false</b> if an 
  2537. attempt is made to remove a node from an empty list, 
  2538. and returns <b>true</b> if the removal is successful. The 
  2539. function consists of several steps:<br>
  2540. 1. Instantiate <b>tempPtr</b> as a copy of <b>firstPtr</b>. Eventually, 
  2541. <b>tempPtr</b> will be used to delete the memory space for 
  2542. the node being removed.<br>
  2543. 2. If <b>firstPtr</b> is equal to <b>lastPtr</b>, i.e., if the list has only 
  2544. one element prior to the removal attempt, then set 
  2545. <b>firstPtr</b> and <b>lastPtr</b> to zero to dethread that node from 
  2546. the list (leaving the list empty).<br>
  2547.  
  2548. </page>
  2549. <page>
  2550. 3. If the list has more than one node prior to removal, 
  2551. then leave <b>lastPtr</b> as is and simply set <b>firstPtr</b> to 
  2552. <b>firstPtr->nextPtr</b>, i.e., modify <b>firstPtr</b> to point to what 
  2553. was the second node prior to removal (and now, the 
  2554. new first node).<br>
  2555. 4. After all these pointer manipulations are complete, 
  2556. copy to reference parameter <b>value</b> the <b>data</b> member of 
  2557. the node being removed.<br>
  2558. 5. Now <b>delete</b> the memory space for the node pointed 
  2559. to by <b>tempPtr</b>.<br>
  2560. 6. Return <b>true</b> indicating successful removal.<br>
  2561.  
  2562. </page>
  2563. <page>
  2564. <a href="^Illustration::c:s0p6"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.7</a> illustrates function <b>removeFromFront</b>. 
  2565. Part a) illustrates the list before the removal operation. 
  2566. Part b) shows actual pointer manipulations.<br>
  2567. <spacer width=16 height=1>Function <b>removeFromBack</b> (<a href="^Illustration::c:s0p7"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.8</a>) removes the 
  2568. back node of the list and copies the node value to the 
  2569. reference parameter. The function returns <b>false</b> if an 
  2570. attempt is made to remove a node from an empty list, 
  2571. and returns <b>true</b> if the removal is successful. The 
  2572. function consists of several steps:<br>
  2573.  
  2574. </page>
  2575. <page>
  2576. 1. Instantiate <b>tempPtr</b> as a copy of <b>lastPtr</b>. Eventually, 
  2577. <b>tempPtr</b> will be used to delete the memory space for 
  2578. the node being remove.<br>
  2579. 2. If <b>firstPtr</b> is equal to <b>lastPtr</b>, i.e., if the list has only 
  2580. one element prior to the removal attempt, then set 
  2581. <b>firstPtr</b> and <b>lastPtr</b> to zero to dethread that node from 
  2582. the list (leaving the list empty).<br>
  2583. 3. If the list has more than one node prior to removal, 
  2584. then instantiate <b>currentPtr</b> as a copy of <b>firstPtr</b>.<br>
  2585. 4. Now "walk the list" with <b>currentPtr</b> until it points to 
  2586. the node before the last node. This is done with a <b>while</b> 
  2587. loop that keeps replacing <b>currentPtr</b> by <b>currentPtr-
  2588. >nextPtr</b> while <b>currentPtr->nextPtr</b> is not <b>lastPtr</b>.<br>
  2589.  
  2590. </page>
  2591. <page>
  2592. 5. Copy <b>currentPtr</b> to <b>lastPtr</b> to dethread the back 
  2593. node from the list.<br>
  2594. 6. Set the <b>currentPtr->nextPtr </b>to zero in the new last 
  2595. node of the list.<br>
  2596. 7. After all the pointer manipulations are complete, 
  2597. copy to reference parameter <b>value</b> the <b>data</b> member of 
  2598. the node being removed.<br>
  2599. 8. Now <b>delete</b> the memory space for the node pointed 
  2600. to by <b>tempPtr</b>.<br>
  2601. 9. Return <b>true</b> indicating successful removal.<br>
  2602. <a href="^Illustration::c:s0p7"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.8</a> illustrates function <b>removeFromFront</b>. 
  2603. Part a) of the figure illustrates the list before the <br>
  2604.  
  2605. </page>
  2606. <page>
  2607. removal operation. Part b) of the figure shows the 
  2608. actual pointer manipulations. <br>
  2609. <spacer width=16 height=1>Function <b>print</b> first determines if the list is empty. If so, 
  2610. <b>print</b> prints "<b>The list is empty</b>" and terminates. 
  2611. Otherwise, it prints the data in the list. The function 
  2612. initializes <b>currentPtr</b> as a copy of <b>firstPtr</b> and then 
  2613. prints the string "<b>The list is: </b>". While <b>currentPtr</b> is 
  2614. not null, <b>currentPtr->data</b> is printed and the value of 
  2615. <b>currentPtr->nextPtr </b>is assigned to <b>currentPtr</b>. Note 
  2616. that if the link in the last node of the list is not null, the 
  2617. printing algorithm will erroneously print past the end of 
  2618. the list. The printing algorithm is identical for linked 
  2619. lists, stacks, and queues. <br>
  2620.  
  2621. </page>
  2622. <page>
  2623. The kind of linked list we have been discussing is a 
  2624. <i>singly-linked list</i>--the list begins with a pointer to the 
  2625. first node and each node contains a pointer to the next 
  2626. node "in sequence." This list terminates with a node 
  2627. whose pointer member is <b>0</b>. A singly-linked list may be 
  2628. traversed in only one direction.<br>
  2629. <spacer width=16 height=1>A <i>circular, singly-linked list</i> begins with a pointer to the 
  2630. first node and each node contains a pointer to the next 
  2631. node. The "last node" does not contain a <b>0</b> pointer; 
  2632. rather, the pointer in the last node points back to the 
  2633. first node, thus closing the "circle."<br>
  2634. <spacer width=16 height=1>A <i>doubly-linked list</i> allows traversals both forwards and 
  2635. backwards. Such a list is often implemented with two <br>
  2636.  
  2637. </page>
  2638. <page>
  2639. "start pointers"--one that points to the first element of 
  2640. the list to allow front-to-back traversal of the list, and 
  2641. one that points to the last element of the list to allow 
  2642. back-to-front traversal of the list. Each node has both a 
  2643. forward pointer to the next node in the list in the 
  2644. forward direction and a backward pointer to the next 
  2645. node in the list in the backward direction. If your list 
  2646. contains an alphabetized telephone directory, for 
  2647. example, searching for someone whose name begins 
  2648. with a letter near the front of the alphabet might begin 
  2649. from the front of the list. Searching for someone whose 
  2650. name begins with a letter near the end of the alphabet 
  2651. might begin from the back of the list. <br>
  2652.  
  2653. </page>
  2654. <page>
  2655. In a <i>circular, doubly-linked list</i>, the forward pointer of 
  2656. the last node points to the first node and the backward 
  2657. pointer of the first node points to the last node, thus 
  2658. closing the "circle."<br>
  2659.  
  2660. </page>
  2661. <page>
  2662. <b>Select the true statement(s). </b><br>
  2663. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2664. A circular, singly-linked list begins with a pointer to the first node and each node contains a pointer to the next node. The last node points to the first node.  <br>
  2665. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A linked list is a linear collection of self-referential class objects connected by pointer links.">
  2666. A linked list is a non-linear collection of self-referential class objects connected by pointer links.   <br>
  2667. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2668. A node in a list can contain data of any type.  <br>
  2669. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2670.  
  2671. </page>
  2672. </section>
  2673. <section type=Body name=Default title="15.5 Stacks">
  2674. <page>
  2675. <font size=18 bold>15.5 Stacks</font><hr>
  2676. In Chapter 12, "Templates," we explained the notion of 
  2677. a stack class template with an underlying array 
  2678. implementation. In this section, we use an underlying 
  2679. pointer-based linked-list implementation. We also 
  2680. discuss stacks in Chapter 20, "Standard Template 
  2681. Library (STL)."<br>
  2682. <spacer width=16 height=1>A <i>stack</i> is a constrained version of a linked list--new 
  2683. nodes can be added to a stack and removed from a stack 
  2684. only at the top. For this reason, a stack is referred to as a 
  2685. <i>last-in, first-out (LIFO)</i> data structure. The link member <br>
  2686.  
  2687. </page>
  2688. <page>
  2689. in the last <a href="^Errors::c:s0p6"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>node of the stack is set to null (zero) to 
  2690. indicate the bottom of the stack. <br>
  2691. <spacer width=16 height=1>The primary member functions used to manipulate a 
  2692. stack are <b>push</b> and <b>pop</b>. Function <b>push</b> adds a new 
  2693. node to the top of the stack. Function <b>pop</b> removes a 
  2694. node from the top of the stack, stores the popped value 
  2695. in a reference variable that is passed to the calling 
  2696. function, and returns <b>true</b> if the <b>pop</b> operation was 
  2697. successful (<b>false</b> otherwise). <br>
  2698. <spacer width=16 height=1>Stacks have many interesting applications. For 
  2699. example, when a function call is made, the called 
  2700. function must know how to return to its caller, so the 
  2701. return address is pushed onto a stack. If a series of <br>
  2702.  
  2703. </page>
  2704. <page>
  2705. function calls occurs, the successive return values are 
  2706. pushed onto the stack in last-in, first-out order so that 
  2707. each function can return to its caller. Stacks support 
  2708. recursive function calls in the same manner as 
  2709. conventional nonrecursive calls. <br>
  2710. <spacer width=16 height=1>Stacks contain the space created for automatic variables 
  2711. on each invocation of a function. When the function 
  2712. returns to its caller, the space for that function's 
  2713. automatic variables is popped off the stack, and those 
  2714. variables are no longer known to the program. <br>
  2715. <spacer width=16 height=1>Stacks are used by compilers in the process of 
  2716. evaluating expressions and generating machine 
  2717. language code. The Exercises explore several <br>
  2718.  
  2719. </page>
  2720. <page>
  2721. applications of stacks, including using them to develop 
  2722. a complete working compiler.<br>
  2723. <spacer width=16 height=1>We will take advantage of the close relationship 
  2724. between lists and stacks to implement a stack class 
  2725. primarily by reusing a list class. We use two different 
  2726. forms of reusability. First, we implement the stack class 
  2727. through private inheritance of the list class. Then we 
  2728. implement an identically performing stack class 
  2729. through composition by including a list class as a 
  2730. private member of a stack class. Of course, all of the 
  2731. data structures in this chapter, including these two stack 
  2732. classes, are implemented as templates (see Chapter 12, 
  2733. "Templates") to encourage further reusability.<br>
  2734.  
  2735. </page>
  2736. <page>
  2737. The program of <a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.9</a> (whose output is shown in 
  2738. Fig. 15.10) creates a <b>Stack</b> class template primarily 
  2739. through <b>private</b> inheritance of the <b>List</b> class template 
  2740. <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.3</a>. We want the <b>Stack</b> to have member functions 
  2741. <b>push</b>, <b>pop</b>, <b>isStackEmpty</b> and <b>printStack</b>. Note that 
  2742. these are essentially the <b>insertAtFront</b>, 
  2743. <b>removeFromFront</b>, <b>isEmpty</b> and <b>print</b> functions of 
  2744. the <b>List</b> class template. Of course, the <b>List</b> class 
  2745. template contains other member functions (i.e., 
  2746. <b>insertAtBack</b> and <b>removeFromBack</b>) that we would 
  2747. not want to make accessible through the <b>public</b> 
  2748. interface to the <b>Stack</b> class. So when we indicate that 
  2749. the <b>Stack</b> class template is to inherit the <b>List</b> class <br>
  2750.  
  2751. </page>
  2752. <page>
  2753. template, we specify <b>private</b> inheritance. This makes 
  2754. all the <b>List</b> class template's member functions <b>private</b> 
  2755. in the <b>Stack</b> class template. When we implement the 
  2756. <b>Stack</b>'s member functions, we then simply have each of 
  2757. these call the appropriate member function of the <b>List</b> 
  2758. class--<b>push</b> calls <b>insertAtFront</b>, <b>pop</b> calls 
  2759. <b>removeFromFront</b>, <b>isStackEmpty</b> calls <b>isEmpty</b> and 
  2760. <b>printStack</b> calls <b>print</b>.<br>
  2761. <spacer width=16 height=1>The stack class template is used in <b>main</b> to instantiate 
  2762. integer stack <b>intStack</b> of type <b>Stack< int ></b>. Integers 0 
  2763. through 3 are pushed onto <b>intStack</b> and then popped off 
  2764. <b>intStack</b>. The stack class template is then used to 
  2765. instantiate float stack <b>doubleStack</b> of type <b>Stack< 
  2766. </b><br>
  2767.  
  2768. </page>
  2769. <page>
  2770. <b>double ></b>. Values 1.1, 2.2, 3.3 and 4.4 are pushed onto 
  2771. <b>doubleStack</b> and then popped off <b>doubleStack</b>.<br>
  2772. <spacer width=16 height=1>Another way to implement a <b>Stack</b> class template is by 
  2773. reusing a <b>List</b> class template through composition. The 
  2774. program of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.11</a> uses the files l<b>ist.h </b>and <b>listnd.h</b> 
  2775. from the <b>List</b> program. It also uses the same driver 
  2776. program as the previous <b>Stack</b> program, except the new 
  2777. header file--<b>stack_c.h</b>--is included and replaces 
  2778. <b>stack.h</b>. The output is also the same. The <b>Stack</b> class 
  2779. template definition now includes member object <b>s</b> of 
  2780. type <b>List< STACKTYPE ></b>.<br>
  2781.  
  2782. </page>
  2783. <page>
  2784. <b>Select the true statement(s). </b><br>
  2785. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A stack is a last-in, first-out (LIFO) data structure.">
  2786. A stack is a first-in, first-out (FIFO) data structure.   <br>
  2787. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2788. The primary operations used to manipulate a stack are push and pop.  <br>
  2789. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2790.  
  2791. </page>
  2792. </section>
  2793. <section type=Body name=Default title="15.6 Queues">
  2794. <page>
  2795. <font size=18 bold>15.6 Queues</font><hr>
  2796. A <i>queue</i> is similar to a supermarket checkout line--the 
  2797. first person in line is serviced first and other customers 
  2798. enter the line at the end and wait to be serviced. Queue 
  2799. nodes are removed only from the <i>head</i> of the queue and 
  2800. are inserted only at the <i>tail</i> of the queue. For this 
  2801. reason, a queue is referred to as a <i>first-in, first-out 
  2802. (FIFO)</i> data structure. The insert and remove 
  2803. operations are known as <b><i>enqueue</i></b> and <b><i>dequeue</i></b>. <br>
  2804. <spacer width=16 height=1>Queues have many applications in computer systems. 
  2805. Most computers have only a single processor, so only 
  2806. one user at a time can be serviced. Entries for the other <br>
  2807.  
  2808. </page>
  2809. <page>
  2810. users are placed in a queue. Each entry gradually 
  2811. advances to the front of the queue as users receive 
  2812. service. The entry at the front of the queue is the next to 
  2813. receive service.<br>
  2814. <spacer width=16 height=1>Queues are also used to support print spooling. A 
  2815. multiuser environment may have only a single printer. 
  2816. Many users may be generating outputs to be printed. If 
  2817. the printer is busy, other outputs may still be generated. 
  2818. These are "spooled" to disk (much as thread is wound 
  2819. onto a spool) where they wait in a queue until the 
  2820. printer becomes available.<br>
  2821. <spacer width=16 height=1>Information packets also wait in queues in computer 
  2822. networks. Each time a packet arrives at a network node, <br>
  2823.  
  2824. </page>
  2825. <page>
  2826. it must be routed to the next node on the network along 
  2827. the path to the packet's final destination. The routing 
  2828. node routes one packet at a time, so additional packets 
  2829. are enqueued until the router can route them. <br>
  2830. <spacer width=16 height=1><a href="^Errors::c:s0p7"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>A file server in a computer network handles file access 
  2831. requests from many clients throughout the network. 
  2832. Servers have a limited capacity to service requests from 
  2833. clients. When that capacity is exceeded, client requests 
  2834. wait in queues.<br>
  2835. <spacer width=16 height=1>The program of <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.12</a> (whose output is shown in 
  2836. Fig. 15.13) creates a <b>Queue</b> class template primarily 
  2837. through <b>private</b> inheritance of the <b>List</b> class template 
  2838. of <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig 15.3</a>. We want the <b>Queue</b> to have member <br>
  2839.  
  2840. </page>
  2841. <page>
  2842. functions <b>enqueue</b>, <b>dequeue</b>, <b>isQueueEmpty</b> and 
  2843. <b>printQueue</b>. We note that these are essentially the 
  2844. <b>insertAtBack</b>, <b>removeFromFront</b>, <b>isEmpty</b> and <b>print</b> 
  2845. functions of the <b>List</b> class template. Of course, the <b>List</b> 
  2846. class template contains other member functions (i.e., 
  2847. <b>insertAtFront</b> and <b>removeFromBack</b>) that we would 
  2848. not want to make accessible through the <b>public</b> 
  2849. interface to the <b>Queue</b> class. So when we indicate that 
  2850. the <b>Queue</b> class template is to inherit the <b>List</b> class 
  2851. template, we specify <b>private</b> inheritance. This makes 
  2852. all the <b>List</b> class template's member functions <b>private</b> 
  2853. in the <b>Queue</b> class template. When we implement the 
  2854. <b>Queue</b>'s member functions, we simply have each of <br>
  2855.  
  2856. </page>
  2857. <page>
  2858. these call the appropriate member function of the list 
  2859. class--<b>enqueue</b> calls <b>insertAtBack</b>, <b>dequeue</b> calls 
  2860. <b>removeFromFront</b>, <b>isQueueEmpty</b> calls <b>isEmpty</b> and 
  2861. <b>printQueue</b> calls <b>print</b>.<br>
  2862. <spacer width=16 height=1>The queue class template is used in <b>main</b> to instantiate 
  2863. integer queue <b>intQueue</b> of type <b>Queue< int ></b>. Integers 
  2864. 0 through 3 are enqueued to <b>intQueue</b> and then 
  2865. dequeued from <b>intQueue</b> in first-in-first-out order. The 
  2866. queue class template is then used to instantiate float 
  2867. queue <b>doubleQueue</b> of type <b>Queue< double ></b>. Values 
  2868. 1.1, 2.2, 3.3 and 4.4 are enqueued to <b>doubleQueue</b> and 
  2869. then dequeued from <b>doubleQueue</b> in first-in-first-out 
  2870. order.<br>
  2871.  
  2872. </page>
  2873. <page>
  2874. <b>Select the true statement(s). </b><br>
  2875. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2876. A queue is a first-in, first-out (FIFO) data structure.  <br>
  2877. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2878. The primary operations used to manipulate a queue are enqueue and dequeue.  <br>
  2879. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2880.  
  2881. </page>
  2882. </section>
  2883. <section type=Body name=Default title="15.7 Trees">
  2884. <page>
  2885. <font size=18 bold>15.7 Trees</font><hr>
  2886. Linked lists, stacks, and queues are <i>linear data 
  2887. structures</i>. A tree is a nonlinear, two-dimensional data 
  2888. structure with special properties. Tree nodes contain 
  2889. two or more links. This section discusses <i>binary trees</i> 
  2890. (<a href="^Illustration::c:s0p9"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.14</a>)--trees whose nodes all contain two links 
  2891. (none, one, or both of which may be null). The <i>root 
  2892. node</i> is the first node in a tree. Each link in the root 
  2893. node refers to a <i>child</i>. The <i>left child</i> is the root node of 
  2894. the <i>left subtree</i>, and the <i>right child </i>is the root node of 
  2895. the <i>right subtree</i>. The children of a node are called 
  2896. si<i>blings</i>. A node with no children is called a <i>leaf node</i>. <br>
  2897.  
  2898. </page>
  2899. <page>
  2900. Computer scientists normally draw trees from the root 
  2901. node down--exactly the opposite of trees in nature.<br>
  2902. <spacer width=16 height=1>In this section, a special binary tree called a <i>binary 
  2903. search tree</i> is created. A binary search tree (with no 
  2904. duplicate node values) has the characteristic that the 
  2905. values in any left subtree are less than the value in its 
  2906. parent node, and the values in any right subtree are 
  2907. greater than the value in its parent node. <a href="^Illustration::c:s0p8"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 15.15</a> 
  2908. illustrates a binary search tree with 12 values.  <a href="^Errors::c:s0p8"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Note that 
  2909. the shape of the binary search tree that corresponds to a 
  2910. set of data can vary, depending on the order in which 
  2911. the values are inserted into the tree.<br>
  2912.  
  2913. </page>
  2914. <page>
  2915. The program of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 15.16</a> (whose output is shown in 
  2916. Fig. 15.17) creates a binary search tree and traverses it 
  2917. (i.e., walks through all its nodes) three ways--using 
  2918. recursive <i>inorder</i>, <i>preorder</i>, and <i>postorder traversals</i>. <br>
  2919. <spacer width=16 height=1>Function <b>main</b> begins by instantiating integer tree 
  2920. <b>intTree</b> of type <b>Tree<int></b>. The program prompts for 10 
  2921. integers, each of which is inserted in the binary tree 
  2922. through a call to <b>insertNode</b>. The program then 
  2923. performs preorder, inorder and postorder traversals 
  2924. (these are explained shortly) of <b>intTree</b>. The program 
  2925. then instantiates floating-point tree <b>doubleTree</b> of type 
  2926. <b>Tree<double></b>. The program prompts for 10 <b>double</b> 
  2927. values, each of which is inserted in the binary tree <br>
  2928.  
  2929. </page>
  2930. <page>
  2931. through a call to <b>insertNode</b>. The program then 
  2932. performs preorder, inorder, and postorder traversals of 
  2933. <b>doubleTree</b>.<br>
  2934. <spacer width=16 height=1>Now, we discuss the class template definitions. We 
  2935. begin with the <b>TreeNode</b> class template that declares as 
  2936. its friend the <b>Tree</b> class template. Class <b>TreeNode</b> has 
  2937. as <b>private</b> data the node's <b>data</b> value, and pointers 
  2938. <b>leftPtr</b> (to the node's left subtree) and <b>rightPtr</b> (to the 
  2939. node's right subtree). The constructor sets <b>data</b> to the 
  2940. value supplied as a constructor argument, and sets 
  2941. pointers <b>leftPtr</b> and <b>rightPtr</b> to zero (thus initializing 
  2942. this node to be a leaf node). Member function <b>getData</b> 
  2943. returns the <b>data</b> value.<br>
  2944.  
  2945. </page>
  2946. <page>
  2947. The <b>Tree</b> class has as <b>private</b> data <b>rootPtr</b>, a pointer to 
  2948. the root node of the tree. The class has public member 
  2949. functions <b>insertNode</b> (that inserts a new node in the 
  2950. tree,) and <b>preorderTraversal</b>, <b>inorderTraversal</b> and 
  2951. <b>postorderTraversal</b>, each of which walks the tree in 
  2952. the designated manner. Each of these member functions 
  2953. calls its own separate recursive utility function to 
  2954. perform the appropriate operations on the internal 
  2955. representation of the tree. The <b>Tree</b> constructor 
  2956. initializes <b>rootPtr</b> to zero to indicate that the tree is 
  2957. initially empty. <br>
  2958. <spacer width=16 height=1>The <b>Tree</b> class's utility function <b>insertNodeHelper</b> 
  2959. recursively inserts a node into the tree. <i>A node can only 
  2960. </i><br>
  2961.  
  2962. </page>
  2963. <page>
  2964. <i>be inserted as a leaf node in a binary search tree</i>. If the 
  2965. tree is empty, a new <b>TreeNode</b> is created, initialized, 
  2966. and inserted in the tree. <br>
  2967. <spacer width=16 height=1>If the tree is not empty, the program compares the value 
  2968. to be inserted with the <b>data</b> value in the root node. If 
  2969. the insert value is smaller, the program recursively calls 
  2970. <b>insertNodeHelper</b> to insert the value in the left subtree. 
  2971. If the insert value is larger, the program recursively 
  2972. calls <b>insertNodeHelper</b> to insert the value in the right 
  2973. subtree. If the value to be inserted is identical to the 
  2974. data value in the root node, the program prints the 
  2975. message "<b> dup</b>" and returns without inserting the 
  2976. duplicate value into the tree.<br>
  2977.  
  2978. </page>
  2979. <page>
  2980. Each of the member functions <b>inOrderTraversal</b>, 
  2981. <b>preOrderTraversal</b>, and <b>postOrderTraversal</b> traverse 
  2982. the tree (<a href="^Illustration::c:s0p10"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.18</a>) and print the node values. <br>
  2983. <spacer width=16 height=1>The steps for an <b>inOrderTraversal</b> are:<br>
  2984. 1. Traverse the left subtree with an <b>inOrderTraversal</b>.<br>
  2985. 2. Process the value in the node (i.e., print the node 
  2986. value).<br>
  2987. 3. Traverse the right subtree with an <b>inOrderTraversal</b>.<br>
  2988. The value in a node is not processed until the values in 
  2989. its left subtree are processed. The <b>inOrderTraversal</b> of 
  2990. the tree in <a href="^Illustration::c:s0p10"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.18</a> is:<br>
  2991. <font size=2><br></font><font size=11><pre>
  2992. 6 13 17 27 33 42 48<p>
  2993. </pre></font>
  2994.  
  2995. </page>
  2996. <page>
  2997. Note that the <b>inOrderTraversal</b> of a binary search tree 
  2998. prints the node values in ascending order. The process 
  2999. of creating a binary search tree actually sorts the data--
  3000. and thus this process is called the <i>binary tree sort</i>.<br>
  3001. <spacer width=16 height=1>The steps for a <b>preOrderTraversal</b> are:<br>
  3002. 1. Process the value in the node.<br>
  3003. 2. Traverse the left subtree with a <b>preOrderTraversal</b>. <br>
  3004. 3. Traverse the right subtree with a <b>preOrderTraversal</b>.<br>
  3005. The value in each node is processed as the node is 
  3006. visited. After the value in a given node is processed, the 
  3007. values in the left subtree are processed, then the values <br>
  3008.  
  3009. </page>
  3010. <page>
  3011. in the right subtree are processed. The 
  3012. <b>preOrderTraversal</b> of the tree in <a href="^Illustration::c:s0p10"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.18</a> is:<br>
  3013. <font size=2><br></font><font size=11><pre>
  3014. 27 13 6 17 42 33 48<p>
  3015. </pre></font>
  3016. The steps for a <b>postOrderTraversal</b> are:<br>
  3017. 1. Traverse the left subtree with a <b>postOrderTraversal</b>.<br>
  3018. 2. Traverse the right subtree with a <b>postOrderTraversal</b>.<br>
  3019. 3. Process the value in the node. <br>
  3020. The value in each node is not printed until the values of 
  3021. its children are printed. The <b>postOrderTraversal</b> of the 
  3022. tree in <a href="^Illustration::c:s0p10"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 15.18</a> is:<br>
  3023. <font size=2><br></font><font size=11><pre>
  3024. 6 17 13 33 48 42 27<p>
  3025. </pre></font>
  3026.  
  3027. </page>
  3028. <page>
  3029. The binary search tree facilitates <i>duplicate elimination</i>. 
  3030. As the tree is being created, an attempt to insert a 
  3031. duplicate value will be recognized because a duplicate 
  3032. will follow the same "go left" or "go right" decisions on 
  3033. each comparison as the original value did. Thus, the 
  3034. duplicate will eventually be compared with a node 
  3035. containing the same value. The duplicate value may 
  3036. simply be discarded at this point.<br>
  3037. Searching a binary tree for a value that matches a key 
  3038. value is also fast. If the tree is tightly packed, then each 
  3039. level contains about twice as many elements as the 
  3040. previous level. So a binary search tree with <i>n</i> elements 
  3041. would have a maximum of <i>log2n</i> levels, and thus a <br>
  3042.  
  3043. </page>
  3044. <page>
  3045. maximum of <i>log2n</i> comparisons would have to be made 
  3046. either to find a match or to determine that no match 
  3047. exists. This means, for example, that when searching a 
  3048. (tightly packed) 1000-element binary search tree, no 
  3049. more than 10 comparisons need to be made because 210 
  3050. > 1000. When searching a (tightly packed) 1,000,000-
  3051. element binary search tree, no more than 20 
  3052. comparisons need to be made because 220 > 1,000,000.<br>
  3053. <spacer width=16 height=1>In the Exercises, algorithms are presented for several 
  3054. other binary tree operations such as deleting an item 
  3055. from a binary tree, printing a binary tree in a two-
  3056. dimensional tree format, and performing a level-order 
  3057. traversal of a binary tree. The level-order traversal of a <br>
  3058.  
  3059. </page>
  3060. <page>
  3061. binary tree visits the nodes of the tree row-by-row 
  3062. starting at the root node level. On each level of the tree, 
  3063. the nodes are visited from left to right. Other binary tree 
  3064. exercises include allowing a binary search tree to 
  3065. contain duplicate values, inserting string values in a 
  3066. binary tree, and determining how many levels are 
  3067. contained in a binary tree.<br>
  3068.  
  3069. </page>
  3070. <page>
  3071. <b>Select the true statement(s). </b><br>
  3072. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  3073. A tree is a non-linear data structure.  <br>
  3074. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  3075. Tree nodes can contain two or more links.  <br>
  3076. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  3077. The root node is the first node in the tree.  <br>
  3078. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A leaf node does not contain child nodes.">
  3079. A leaf node is a node containing children.   <br>
  3080. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The  traversal of a binary search tree traverses the elements in sorted order.">
  3081. The preorder traversal of a binary search tree traverses the elements of the tree in sorted order.inorder  <br>
  3082. <component type=button name=b label="Check Your Answer" width=125 height=24>
  3083.  
  3084. </page>
  3085. </section>
  3086. <section type=Body name=Default title="15.8 Summary">
  3087. <page>
  3088. <font size=18 bold>15.8 Summary</font><hr>
  3089. <indent width=8 delay>*   Self-referential classes contain members called links 
  3090. that point to objects of the same class type.</indent>
  3091. <indent width=8 delay>*   Self-referential classes enable many class objects to 
  3092. be linked together in stacks, queues, lists, and trees. </indent>
  3093. <indent width=8 delay>*   Dynamic memory allocation reserves a block of 
  3094. bytes in memory to store an object during program execution. </indent>
  3095. <indent width=8 delay>*   A linked list is a linear collection of self-referential 
  3096. class objects. </indent>
  3097. <indent width=8 delay>*   A linked list is a dynamic data structure--the length 
  3098. of the list can increase or decrease as necessary.</indent>
  3099.  
  3100. </page>
  3101. <page>
  3102. <indent width=8 delay>*   Linked lists can continue to grow until memory is 
  3103. exhausted. </indent>
  3104. <indent width=8 delay>*   Linked lists provide a mechanism for simple insertion and deletion of data by pointer manipulation.</indent>
  3105. <indent width=8 delay>*   A <i>singly-linked list</i> begins with a pointer to the first 
  3106. node and each node contains a pointer to the next node 
  3107. "in sequence." This list terminates with a node whose 
  3108. pointer member is 0. A singly-linked list may be traversed in only one direction.</indent>
  3109. <indent width=8 delay>*   A <i>circular, singly-linked list</i> begins with a pointer to 
  3110. the first node and each node contains a pointer to the 
  3111. next node. The pointer in the last node points back to 
  3112. the first node, thus closing the "circle."</indent>
  3113.  
  3114. </page>
  3115. <page>
  3116. <indent width=8 delay>*   A <i>doubly-linked list</i> allows traversals both forwards 
  3117. and backwards. Each node has both a forward pointer to 
  3118. the next node in the list in the forward direction, and a 
  3119. backward pointer to the next node in the list in the 
  3120. backward direction. </indent>
  3121. <indent width=8 delay>*   In a <i>circular, doubly-linked list</i>, the forward pointer 
  3122. of the last node points to the first node and the backward pointer of the first node points to the last node, 
  3123. thus closing the "circle."</indent>
  3124. <indent width=8 delay>*   Stacks and queues are constrained versions of linked 
  3125. lists. </indent>
  3126. <indent width=8 delay>*  New stack nodes are added to a stack and are 
  3127. removed from a stack only at the top of the stack. For </indent>
  3128.  
  3129. </page>
  3130. <page>
  3131. <indent width=8 delay>*   this reason, a stack is referred to as a last-in, first-out 
  3132. (LIFO) data structure. </indent>
  3133. <indent width=8 delay>*   The link member in the last node of the stack is set to 
  3134. null (zero) to indicate the bottom of the stack.</indent>
  3135. <indent width=8 delay>*   The two primary operations used to manipulate a 
  3136. stack are <b>push</b> and <b>pop</b>. The <b>push</b> operation creates a 
  3137. new node and places it on the top of the stack. The <b>pop</b> 
  3138. operation removes a node from the top of the stack, 
  3139. deletes the memory that was allocated to the popped 
  3140. node, and returns the popped value. </indent>
  3141. <indent width=8 delay>*  In a queue data structure, nodes are removed from 
  3142. the head and added to the tail. For this reason, a queue 
  3143. is referred to as a first-in, first-out (FIFO) data struc</indent>
  3144.  
  3145. </page>
  3146. <page>
  3147. <indent width=8 delay>*   ture. The add and remove operations are known as 
  3148. <b>enqueue</b> and <b>dequeue</b>. </indent>
  3149. <indent width=8 delay>*   Trees are two-dimensional data structures requiring 
  3150. two or more links per node. </indent>
  3151. <indent width=8 delay>*   Binary trees contain two links per node.</indent>
  3152. <indent width=8 delay>*   The root node is the first node in the tree. </indent>
  3153. <indent width=8 delay>*   Each of the pointers in the root node refers to a child. 
  3154. The left child is the first node in the left subtree, and the 
  3155. right child is the first node in the right subtree. The children of a node are called siblings. Any tree node that 
  3156. does not have any children is called a leaf node. </indent>
  3157. <indent width=8 delay>*  A binary search tree has the characteristic that the 
  3158. value in the left child of a node is less than the value in </indent>
  3159.  
  3160. </page>
  3161. <page>
  3162. <indent width=8 delay>*   its parent node, and the value in the right child of a node 
  3163. is greater than or equal to the value in its parent node. If 
  3164. there are no duplicate data values, the value in the right 
  3165. child is simply greater than the value in its parent node. </indent>
  3166. <indent width=8 delay>*   An inorder traversal of a binary tree traverses the left 
  3167. subtree inorder, processes the value in the root node, 
  3168. then traverses the right subtree inorder. The value in a 
  3169. node is not processed until the values in its left subtree 
  3170. are processed. </indent>
  3171. <indent width=8 delay>*   A preorder traversal processes the value in the root 
  3172. node, traverses the left subtree preorder, then traverses 
  3173. the right subtree preorder. The value in each node is 
  3174. processed as the node is encountered. </indent>
  3175.  
  3176. </page>
  3177. <page>
  3178. <indent width=8 delay>*   A postorder traversal traverses the left subtree postorder, traverses the right subtree postorder, then processes the value in the root node. The value in each 
  3179. node is not processed until the values in both its subtrees are processed.</indent>
  3180.  
  3181. </page>
  3182. <page>
  3183. <br>
  3184.  
  3185. </page>
  3186. </section>
  3187.  
  3188. <section type=Popup name=Debug title="Testing">
  3189. <page>
  3190. This chapter does not contain any Testing and Debugging tips.
  3191. </page>
  3192. </section>
  3193. <section type=Popup name=AppletPopup title="Applet Examples">
  3194. <page>
  3195. This chapter does not contain any Applet Examples.
  3196. </page>
  3197. </section>
  3198. <section type=Popup name=Engineer title="Engineering">
  3199. <page>
  3200. This chapter does not contain any Software Engineering Observations.
  3201. </page>
  3202. </section>
  3203. </chapter>
  3204. </html>
  3205. </html>
  3206.